How to use echo in PHP 8


The echo language construct outputs one or more strings in PHP 8.

echo in PHP 8

echo(string ...$expressions): void


echo can take several expressions at once and output them in PHP at the same time. The language construct won't insert spaces, new line characters or similar charaters between several expressions. The same holds for subsequent echo calls.

  1. $expressions

    This parameters consists of a list of expressions separated by commas. The expression is not enclosed with parentheses.

Unlike most functions or other language constructs in PHP 8, echo does not return a value after calling it.

echo shortcut in PHP 8

Hello !

If you are writing using a PHP file mixed with HTML as output, you can use PHP 8 echo shortcut to output variables within the markup. This saves a few keystrokes by shortening the php script tag and the echo keyword.


echo str_replace("test", "world", "Hello test!");

Hello world!

In this example, the str_replace function replaces the word "test" with the word "world" and outputs the final string "Hello world!".

echo "hello", " test";

hello test

In this example, the echo language constructs prints two expression. These expressions are two strings which are printed onto the same line.