echo sends one or more expressions to the output. PHP does not add a space or a newline between them. echo "hello", "world"; prints helloworld.
echo is a language construct, not a function. The arguments are a comma list after the keyword. It has no return value, so it cannot sit where PHP expects an expression. Non-string values are cast to string, even when strict_types is on. echo 6 * 7; prints 42.
Not an expression.
($ok) ? echo "yes" : echo "no"; does not parse. Pass the ternary in: echo $ok ? "yes" : "no";.
Syntax
PHP Syntax
A comma list, or the short echo tag inside a template. The short tag stays available when short_open_tag is Off.
One or more expressions, separated by commas. PHP casts a non-string to string before sending it.
Reference
Return Value
Result
Meaning
No value
echo cannot be assigned or used as a condition
print returns 1
print takes one expression and can appear inside a larger expression
Commas also dodge concatenation precedence. echo "Sum: ", 1 + 2; prints Sum: 3 because 1 + 2 is its own argument. With the dot, write the parentheses yourself: echo "Sum: " . (1 + 2);.
Code
PHP Examples
Three scripts. Run them with php echo.php.
Example 1 — Comma arguments
1 + 2 is its own expression. echo does not insert a space between arguments.
'Hello, $name' prints the dollar sign and the name of the variable.
<?= $name ?> is short echo. It does not depend on short_open_tag.
Frequently Asked Questions
No. echo is a language construct. It has no return value, so you cannot write $out = echo "hi"; or call it through a variable function. Named arguments do not apply.
The parentheses belong to the expression, not to a function call. PHP reads echo(1 + 2) * 3 as echo ((1 + 2) * 3). echo("hello", "world"); is a parse error because ("hello", "world") is not one expression.
echo accepts a comma-separated list of expressions and returns nothing. print accepts one expression and returns 1, so print can sit inside a larger expression. echo is the usual way to send output.
No. <?= $name ?> is always available, even when short_open_tag is Off. It is the same output as <?php echo $name; ?>.