Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

echo

Description

The echo String for PHP output one or more strings.

The echo String for PHP echo is a language construct, not a function, so parentheses are not required..

Syntax

echo(
    string ...$expressions
): void

Parameters

expressions

One or more string expressions to output, separated by commas. Non-string values will be coerced to strings, even when the strict_types directive is enabled.

Return

No value is returned.

Examples

1 · with parentheses

<?

$expression = "with ()";

echo($expression);

?>
with ()

2 · without parentheses

<?

$expression = "without ()";

echo $expression;

?>
without ()

3 · multiple lines

<?

$expression = "multiple
lines";

echo $expression;

?>
multiple
lines

4 · newline character

<?

$expression = "newline\ncharacter";

echo $expression;

?>
newline
character

5 · escape character

<?

$expression = "escape characters \"like this\"";

echo $expression;

?>
escape characters "like this"

6 · double quotes

<?

$var = "variable";

$expression = "variables are expanded inside double quotes: $var";

echo $expression;

?>
variables are expanded inside double quotes: variable

7 · single quotes

<?

$var = "variable";

$expression = 'variables are not expanded inside single quotes: $var';

echo $expression;

?>
variables are not expanded inside single quotes: $var

8 · curly braces

<?

$array = array("key" => "value");

$expression = "array: {$array['key']}";

echo $expression;

?>
array: value

9 · parameters

<?

$expression1 = 'parameters, ';
$expression2 = 'passed ';
$expression3 = 'separately';

echo $expression1, $expression2, $expression3, PHP_EOL, PHP_EOL;

function myfunction($myparameter)
{
    var_export('function');
    return $myparameter;
}

echo myfunction(1), myfunction(2), myfunction(3);

?>
parameters, passed separately

'function'1'function'2'function'3

10 · concatenation

<?

$expression1 = 'concatenation, ';
$expression2 = 'passed ';
$expression3 = 'together';

echo $expression1 . $expression2 . $expression3 . PHP_EOL . PHP_EOL;

function myfunction($myparameter)
{
    var_export('function');
    return $myparameter;
}

echo myfunction(1) . myfunction(2) . myfunction(3);

?>
concatenation, passed together

'function''function''function'123

11 · heredoc

<?

$var = "variable";

echo <<<MYIDENTIFIER
        The heredoc allows output on multiple lines.
        Syntax begins with <<< and an identifier.
        Variables are expanded: $var.
        Syntax ends with an identifier whose placement determines the indentation.
    MYIDENTIFIER;

?>
    The heredoc allows output on multiple lines.
    Syntax begins with <<< and an identifier.
    Variables are expanded: variable.
    Syntax ends with an identifier whose placement determines the indentation.

12 · nowdoc

<?

$var = "variable";

echo <<<'MYIDENTIFIER'
        The nowdoc allows output on multiple lines.
        Syntax begins with <<< and an identifier in single quotes.
        Variables are not expanded: $var.
        Syntax ends with an identifier whose placement determines the indentation.
    MYIDENTIFIER;

?>
    The nowdoc allows output on multiple lines.
    Syntax begins with <<< and an identifier in single quotes.
    Variables are not expanded: $var.
    Syntax ends with an identifier whose placement determines the indentation.

13 · not an expression

<?

$expression = 'expression';

// not valid
//($expression) ? echo 'true' : echo 'false';

// valid
echo $expression ? 'true' : 'false';

?>
true
HomeMenu