One of the most commonly used functions in PHP is
echo(), which is used to output one or more strings to the browser or command-line interface.
echo string1, string2, ..., stringN;
echo() function can accept one or more strings as arguments, separated by commas. The strings can be enclosed in double or single quotes.
This example will output the text “Hello, world!” to the screen.
echo "Hello World!";echo "Hello", " ", "World", "!";
echo() can also be used to output variables:
$name = "John";echo "My name is $name";
In the example above, the output will be “My name is John”.
The concatenation operator (.) joins the string “My name is “ with the variable
It is also possible to use
echo() to output HTML code. For example:
echo '<h1>Welcome to my website!</h1>';
This will output a heading tag to the screen.
Note: HTML and PHP code can both be used within
One important thing to keep in mind when using
echo() is that it does not return a value. In other words, the output of
echo() cannot be assigned to a variable or used in an expression.
For example, the following code will not work:
$result = echo 'Hello, world!';
The output error will be something like this:
Parse error: syntax error, unexpected token "echo" in /home/user/scripts/code.php on line 2
Instead, the value of the function or method should be returned first using the
The following example shows how to use
echo on a returned value: