PHP: Hypertext Preprocessor (original) (raw)
echo
(PHP 4, PHP 5, PHP 7, PHP 8)
echo — Output one or more strings
Description
Outputs one or more expressions, with no additional newlines or spaces.
echo
is not a function but a language construct. Its arguments are a list of expressions following the echo
keyword, separated by commas, and not delimited by parentheses. Unlike some other language constructs, echo
does not have any return value, so it cannot be used in the context of an expression.
echo
also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. This syntax is available even with the short_open_tag configuration setting disabled.
The major differences to print are thatecho
accepts multiple arguments and doesn't have a return value.
Parameters
expressions
One or more string expressions to output, separated by commas. Non-string values will be coerced to strings, even whenthestrict_types directive is enabled.
Return Values
No value is returned.
Examples
Example #1 echo
examples
`<?php
echo "echo does not require parentheses.";// Strings can either be passed individually as multiple arguments or
// concatenated together and passed as a single argument
echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', "\n";
echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";// No newline or space is added; the below outputs "helloworld" all on one line
echo "hello";
echo "world";// Same as above
echo "hello", "world";
echo
"This string spans
multiple lines. The newlines will be
output as well";
echo
"This string spans\nmultiple lines. The newlines will be\noutput as well.";// The argument can be any expression which produces a string
$foo = "example";
echo "foo is foo";//fooisexamplefoo"; // foo is examplefoo";//fooisexamplefruits = ["lemon", "orange", "banana"];
echo implode(" and ", $fruits); // lemon and orange and banana
// Non-string expressions are coerced to string, even if declare(strict_types=1) is used
echo 6 * 7; // 42
// However, the following examples will work:
($some_var) ? print 'true' : print 'false'; // print is also a construct, but
// it is a valid expression, returning 1,
// so it may be used in this context.echo $some_var ? 'true': 'false'; // evaluating the expression first and passing it to echo
?>`
Example #2 echo
is not an expression
<?php // Because echo does not behave as an expression, the following code is invalid. ($some_var) ? echo 'true' : echo 'false'; ?>
Notes
Note: Because this is a language construct and not a function, it cannot be called usingvariable functions, or named arguments.
Note: Using with parentheses
Surrounding a single argument to
echo
with parentheses will not raise a syntax error, and produces syntax which looks like a normal function call. However, this can be misleading, because the parentheses are actually part of the expression being output, not part of theecho
syntax itself.Example #3 Using Parentheses
<?php echo "hello", PHP_EOL; // outputs "hello"echo("hello"), PHP_EOL; // also outputs "hello", because ("hello") is a valid expressionecho(1 + 2) * 3, PHP_EOL; // outputs "9"; the parentheses cause 1+2 to be evaluated first, then 3*3 // the echo statement sees the whole expression as one argumentecho "hello", " world", PHP_EOL; // outputs "hello world"echo("hello"), (" world"), PHP_EOL; // outputs "hello world"; the parentheses are part of each expression ?>
Example #4 Invalid Expression
<?php echo("hello", " world"), PHP_EOL; // Throws a Parse Error because ("hello", " world") is not a valid expression ?>
Tip
Passing multiple arguments to echo
can avoid complications arising from the precedence of the concatenation operator in PHP. For instance, the concatenation operator has higher precedence than the ternary operator, and prior to PHP 8.0.0 had the same precedence as addition and subtraction:
<?php // Below, the expression 'Hello ' . isset($name) is evaluated first, // and is always true, so the argument to echo is always $name echo 'Hello ' . isset($name) ? $name : 'John Doe' . '!';// The intended behaviour requires additional parentheses echo 'Hello ' . (isset($name) ? $name : 'John Doe') . '!';// In PHP prior to 8.0.0, the below outputs "2", rather than "Sum: 3" echo 'Sum: ' . 1 + 2;// Again, adding parentheses ensures the intended order of evaluation echo 'Sum: ' . (1 + 2);
If multiple arguments are passed in, then parentheses will not be required to enforce precedence, because each expression is separate:
`<?php
echo "Hello ", isset($name) ? $name : "John Doe", "!";
echo
"Sum: ", 1 + 2;`
See Also
- print - Output a string
- printf() - Output a formatted string
- flush() - Flush system output buffer
- Ways to specify literal strings
Found A Problem?
pemapmodder1970 at gmail dot com ¶
8 years ago
`Passing multiple parameters to echo using commas (',')is not exactly identical to using the concatenation operator ('.'). There are two notable differences.
First, concatenation operators have much higher precedence. Referring to http://php.net/operators.precedence, there are many operators with lower precedence than concatenation, so it is a good idea to use the multi-argument form instead of passing concatenated strings.
Second, a slightly confusing phenomenon is that unlike passing arguments to functions, the values are evaluated one by one.
The output would be:
string(3) "bar"FoobarFoo
Foostring(3) "bar"
barFoo
It would become a confusing bug for a script that uses blocking functions like sleep() as parameters:
vs
With ',' the cursor stops at the beginning every newline, while with '.' the cursor stops after the 0 in the beginning every line (because sleep() returns 0).
`