Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Language Constructs and Statements

PHP echo statement

Reference for PHP echo: output one or more strings, comma-separated arguments versus concatenation, short echo tags, and how it differs from print.

echo sends one or more values to the output. It is a language construct, not a function, so parentheses are optional and it has no return value. In a command-line script the output goes to the terminal; in a web request it becomes part of the response body.

Values that are not strings are converted to strings first: integers and floats become their decimal text, true becomes "1", false and null become the empty string. Arrays cannot be converted meaningfully (you get the word Array and a warning), so use print_r() or var_dump() for those.

PHP
echo "Hello", " ", "world", "\n";
echo 42, "\n";

Try it

PHP Echo with several arguments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Heredoc, parentheses and print_r

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
echo expression [, expression ...];
<?= expression ?>
Name Description
expression One or more values separated by commas. Each is converted to a string and written in order.

Return value: none. echo cannot be used inside an expression.

Errors: an array argument produces a warning ("Array to string conversion"); an object without __toString() throws an Error.

Usage notes

  • Commas versus dots. echo $a, $b; outputs the values one by one; echo $a . $b; first builds a single string. Both look the same, but remember that . and + have different precedence when mixed with arithmetic (since PHP 8, + binds tighter than .).
  • Parentheses do not make it a function call. echo("a"), ("b"); works, but echo("a", "b"); is a syntax error because ("a", "b") is not an expression.
  • Short echo tag. <?= $x ?> is equivalent to <?php echo $x; ?> and is always available, even when short_open_tag is off. It is the usual way to print values inside HTML templates.
  • Escape output for HTML. In a web page, pass untrusted values through htmlspecialchars() before echoing them; see Escaping output.
  • Floats print with the precision setting (14 significant digits by default for echo, -1 for shortest round-trip in var_dump and var_export), so echo 0.1 + 0.2 shows 0.3.
  • echo versus print. print takes one argument and returns 1, so it can appear inside an expression. echo is marginally lighter and accepts several arguments.
  • Output buffering. If ob_start() is active, echo writes into the buffer instead of sending the text immediately.
  • printf() and sprintf() are better when you need padding, rounding or fixed decimals.