PHP echo outputs one or more strings to the screen. It is not a function but a language construct that is frequently used in PHP. Generally, its syntax looks like
echo(string …$expression) : void
Here are some important points about PHP echo.
- We do not use parenthesis around the $expression arguments. In the syntax above, it is just there to show the expected arguments.
- echo doesn’t have a return value.
- It coerces the non-string value to string and prints it to the screen. That’s why it raises a warning for an array because PHP doesn’t coerce an array to a string.
Here’s an example of an echo with more than one argument. This usage is relatively uncommon.
<?php //OUTPUT: Hello World echo "Hello ","World"; ?>
Now, as we’ve seen what is echo in PHP, we’ll explore it more with examples.
#1 – Printing string with PHP echo
PHP echo takes one or more arguments. Let’s just pass a string expression to it.
<?php //OUTPUT: Hello World! This is a string echo "Hello World! This is a string"; ?>
We can also pass more than one argument, as we’ve already seen. Let’s rewrite the above example that way.
<?php //OUTPUT: Hello World! This is a string echo "Hello ", "World! ","This ","is ","a ","string"; ?>
The change here is that we’ve more than one argument now. There’s another way to write this using the concatenation operator.
<?php //OUTPUT: Hello World! This is a string echo "Hello "."World! "."This "."is "."a "."string"; ?>
We can also use a variable in echo.
<?php $string = "Hello World! This is a variable with a string value"; //OUTPUT: Hello World! This is a variable with a string value echo $string; ?>
#2 – Print escape sequences with PHP echo
Escape sequences are special characters that are not printed as is but they carry a special meaning. The escape sequence starts with a backslash
\ and it can represent a line break, whitespace, tabs, or alerts. They are particularly helpful to print special characters to the screen. Let’s see what that means.
<?php $string = "Hello World! This is a variable with a string value"; //OUTPUT: $string variable has a string value echo "\$string variable has a string value"; ?>
Do you see \$string? If we remove the backslash, the echo statement will print the value of the $string variable rather than text “$string”.
There is a escape sequence
\t that represents tab whitespace. We’ll see how that works.
<?php //OUTPUT: Name Age Job echo "Name\tAge\tJob"; ?>
The space is introduced by
\t escape sequence. We also have a newline character.
<?php //OUTPUT: //Name //Age //Job echo "Name\nAge\nJob"; ?>
\n introduces a line break in a string. There are many other helpful escape characters enlisted in their documentation, PHP: Escape sequences.
#3 – Print array with PHP echo
Printing an array with echo is not straightforward because PHP doesn’t convert an array to string directly. That’s why echo raises an error whenever passed an array. The only way is to convert an array into a string using the implode function and pass it to echo.
That’s all about echo in PHP. We hope you’ve enjoyed this article.
Want to explore more useful PHP tutorials?
We have many fun articles related to PHP. You can explore these to learn more about PHP.