echo prints text. Write echo "Hello"; to output a string, echo "Hello, $name"; to include a variable, and echo $a, $b; to print several values in a row. Whatever echo prints becomes the page the browser shows, or the text you see in a terminal.
The first line uses interpolation (the variable sits inside double quotes), the second joins strings with the . operator, and the third passes three values separated by commas. All three print the same kind of result.
Echo HTML
In a web page, echo usually prints HTML. The browser receives the text and renders the tags. Press Run and the Page tab shows the rendered result:
The last line uses single quotes so the double quotes of class="note" need no escaping. Inside single quotes variables are not replaced, so use them for fixed text. If a value comes from a visitor (a form, the URL), print it through htmlspecialchars so it cannot inject HTML; see the htmlspecialchars page.
The short echo tag <?=
Inside an HTML file, <?= $value ?> is the short form of <?php echo $value; ?>. It keeps templates readable:
The short echo tag is always available. The other short tag, <? without php, depends on a setting and should not be used.
Echo multiple values and new lines
echo does not add a line break. In a terminal, add "\n" (in double quotes) or the constant PHP_EOL. In HTML, a line break in the source does nothing visible; use <br> or separate elements.
Commas and dots give the same output. With commas, echo prints each value in turn; with dots, PHP first builds one string and then prints it. Line breaks have their own page: PHP newline.
Echo vs print
print is the older sibling. It prints one value and always returns 1, which lets it appear inside an expression. echo takes any number of values and returns nothing.
echo | print | |
|---|---|---|
| Values | one or more, comma separated | exactly one |
| Returns | nothing (it is a statement) | 1 |
| Use in an expression | no | yes, $ok = print "x"; |
| Common use | almost everything | rarely |
printf is different again: it formats values with placeholders, like printf("%05.2f", 3.14159), and is covered on the sprintf page.
Echo an array or an object
echo only prints strings and values that convert to strings (numbers, booleans, null). An array does not convert. Your own PHP prints a warning and the word Array:
PHP Warning: Array to string conversion in /home/index.php on line 3
Array
To show the contents, pick the function for the job:
An object is stricter: unless its class defines __toString(), echo $object; stops the script with Error: Object of class User could not be converted to string. Booleans are a common surprise too: echo true; prints 1 and echo false; prints nothing at all. Use var_dump($value) when you need to see the exact type and value.
Common mistake: echo with parentheses and commas
Because echo looks like a function, people write echo("a", "b");. That is a parse error, since the parentheses wrap a single expression:
PHP Parse error: syntax error, unexpected token "," in /home/index.php on line 2
Drop the parentheses (echo "a", "b";) or join the values (echo("a" . "b");). The same applies to print, which never accepts a second value, with or without parentheses.
Frequently Asked Questions
What is the difference between echo and print in PHP?
Both output strings. echo accepts several comma-separated values (echo $a, $b;) and returns nothing; print takes exactly one value and returns 1, so it can be used inside an expression. In practice echo is used almost everywhere.
Do I need parentheses with echo?
No. echo is a language construct, not a function, so echo "Hi"; is the normal form. echo("Hi"); works because the parentheses only group one expression, but echo("a", "b"); is a parse error.
Why does echo false print nothing?
echo converts its value to a string, and false becomes the empty string "" while true becomes "1". To see the boolean itself, use var_dump($x); (prints bool(false)) or echo var_export($x, true); (prints false).
What does <?= mean in PHP?
<?= $value ?> is the short echo tag, identical to <?php echo $value; ?>. It is always enabled since PHP 5.4 and is the usual way to print values inside HTML templates.