echo and print in PHP
echo is a PHP language construct that outputs one or more values in the order you list them. It adds no spaces or line breaks of its own, so every line break in its output comes from the values you give it, such as \n or PHP_EOL. print outputs a single value and gives back the value 1.
Key facts
echoaccepts several comma-separated values, butprint "a", "b";is a parse error becauseprinttakes one.printalways gives back the value 1, which is whyecho print "Hello";outputsHello1.- The truth value
trueechoes as1, whilefalseandnull(no value) echo nothing. PHP_EOLis"\r\n"when PHP runs on Windows and"\n"on Linux and macOS.- Echoing an array outputs the word
Array, not the values inside it, plus aWarning: Array to string conversionwhere warnings are shown.
What does echo do in PHP?
echo outputs the values you give it, one after another, with nothing added. An echo statement can take a single value or a list of values separated by commas, and a value can be text in quotes, a number or a sum such as 2 + 3.
echo "Hello", ", ", "World", "!\n";
echo "Total: ", 2 + 3, "\n";Hello, World! Total: 5
The comma and space in Hello, World come from the ", " string, not from the commas between the values. echo works out each value and outputs it before moving on to the next one. For 2 + 3, PHP adds the numbers and outputs the text 5.
When PHP runs on a web server, that output becomes the page a visitor's browser receives. A value can also be a variable, which the next lesson introduces, and echo prints it the same way.
A dot joins strings into one, so echo "Hello" . ", " . "World"; prints Hello, World as a single value.
What is the difference between echo and print in PHP?
echo takes one or more values and returns nothing, while print takes one value and returns 1. A construct returns a value when it hands that value back to the code that used it. Apart from returning 1, the print statement outputs its value the way echo does.
An expression is any piece of code that produces a value, such as 2 + 3. A print statement produces the value 1 and fits where PHP expects a value. An echo statement produces nothing, and putting one in that spot is a parse error.
| Feature | echo | print |
|---|---|---|
| How many values | One or more, with commas | Exactly one |
| Returns | Nothing | Always 1 |
| Usable as a value | No, a parse error | Yes |
| Short tag | <?= | None |
| Wrapped in ( ) | Yes, one value per pair | Yes |
echo print "Hello", "\n";Hello1
Here print outputs Hello first. Its return value, 1, becomes the first value echo outputs, followed by the line break.
You'll often read that echo is faster than print, because print has to return a value. We had PHP 8.6.0 list the instructions it turns each line into before running it. echo "a"; and print "b"; each became one ECHO instruction. Speed is no reason to pick one over the other.
Can you use parentheses with echo in PHP?
Yes, but one pair of parentheses can hold only one value. The parentheses belong to the value inside them. PHP reads echo("Hello") as echo followed by the value ("Hello").
echo("Hello"), "\n";
echo(2 + 3) * 10, "\n";Hello 50
The second line prints 50, not 5. If echo were a function, (2 + 3) would be its whole input. Instead, echo outputs the full value (2 + 3) * 10.
This line stops at the comma with a parse error, because two values in one pair of parentheses don't make a single value.
echo("Hello", "World");Parse error: syntax error, unexpected token "," in /app/main.php on line 2
Remove the parentheses, as in echo "Hello", "World";, or give each value its own pair. print follows the same rule, which is why print("Hello") . " World"; prints Hello World. It outputs the whole expression ("Hello") . " World", not only the part in parentheses.
How do you echo a new line in PHP?
Where each line should end, put \n inside double quotes or add PHP_EOL as one more value. What to use depends on where the output goes, because a web page handles line breaks differently from a terminal.
| Where the output goes | How to end a line |
|---|---|
| A terminal or the runner | "\n" or PHP_EOL, since both end a line in plain text |
| A text file on the computer that runs PHP | PHP_EOL, which matches that system's line ending |
| A web page | A <br> tag, because a browser shows \n as a space |
| A web page, for text that already has line breaks | nl2br(), which adds <br /> before each line break |
| A web page, with the breaks kept as typed | A <pre> element, which keeps the line breaks inside it |
nl2br() is a built-in function that inserts an HTML line break tag before every line break in a string. The original line breaks stay in the HTML source as well.
echo nl2br("One\nTwo");One<br /> Two
The tag comes out as <br />. Add the truth value false after the string, as in nl2br("One\nTwo", false), to get <br> instead. The function also treats \r\n, the two-character line ending that PHP_EOL holds on Windows, as one line break.
How do you echo several lines at once in PHP?
Let one string run over several lines, or give echo several strings that each end in \n.
echo "Monday
Tuesday
";
echo "Wednesday\n", "Thursday\n";Monday Tuesday Wednesday Thursday
PHP keeps a line break typed inside the quotes, so the first string prints Monday and Tuesday on separate lines. For longer blocks of text, PHP also has heredoc syntax.
What is PHP_EOL in PHP?
PHP_EOL is a built-in constant that holds the line ending of the system running PHP. EOL stands for end of line.
On Windows, PHP_EOL is "\r\n", a carriage return followed by a line feed. PHP's source code sets it to "\n", the line feed alone, on every other system, including Linux and macOS.
echo "Line one", PHP_EOL, "Line two", PHP_EOL;Line one Line two
The runner ends each line with "\n", the same as PHP on Linux and macOS. PHP 8.6 and 8.5 on Windows end each line with "\r\n", and the two lines look the same.
strlen() returns the length of a string. Here it shows how many characters PHP_EOL holds.
echo strlen(PHP_EOL);1
The runner prints 1. On Windows, PHP 8.6 and 8.5 both print 2, one character for the carriage return and one for the line feed.
Reach for PHP_EOL when a file or log should use the line ending of the computer that runs the script. In ordinary web page text it makes no visible difference.
How do you echo HTML in PHP?
Write the HTML tags inside the string, and echo outputs them like any other text.
echo "<h1>Welcome</h1>\n";
echo '<p class="intro">First line<br>Second line</p>', "\n";<h1>Welcome</h1> <p class="intro">First line<br>Second line</p>
The runner shows the tags as text. A browser that receives the same output builds a heading and a paragraph from it, with the <br> breaking the paragraph's line.
The second string uses single quotes, so the double quotes in class="intro" can stay as they are. Single quotes keep \n as plain characters, though, which is why the line break comes as a separate value in double quotes.
If an echoed page looks wrong, paste the output into the HTML validator, which points to unclosed tags and the line each one is on.
Before you echo text that a visitor typed, pass it through htmlspecialchars(). Any tags in that text then appear as text instead of becoming part of your page.
How does the short echo tag <?= work?
The short echo tag <?= outputs the values after it, the same as <?php echo.
It takes several values separated by commas, and the closing ?> ends the statement, which leaves no need for a semicolon. That suits a single value inside a line of HTML.
<ul>
<li> "echo", " and ", "print" </li>
<li>Total: 2 + 3 </li>
</ul><ul> <li>echo and print</li> <li>Total: 5</li> </ul>
This file has no <?php tag, and it doesn't need one, since each <?= opens PHP by itself. The shopping cart tutorial fills its product pages with tags like these.
What does echo print for true, false and null in PHP?
echo true prints 1, while false and null print nothing at all. true and false are PHP's two truth values, and null means no value.
echo "[", true, "]\n";
echo "[", false, "]\n";
echo "[", null, "]\n";[1] [] []
PHP converts each of these values to text before echo outputs it. true becomes 1, while false and null become an empty string, so nothing appears between their brackets.
To see whether a value is really false or null while testing, use var_dump() instead. It prints bool(false) or NULL.
What happens when you echo an array in PHP?
PHP outputs the word Array and raises a warning, instead of printing the values inside the array.
An array holds a list of values, written in square brackets. PHP converts any array to the text Array, so neither echo nor print can show what's in it.
echo ["apple", "banana"];Warning: Array to string conversion in /app/main.php on line 2 Array
The runner shows the warning, as PHP's development settings do. A live site that uses the php.ini-production settings keeps it off the page and shows only Array.
Code after the warning still runs. To look inside an array, use print_r(), which a later lesson on arrays covers in full.
Common mistakes with echo and print in PHP
A misplaced comma in echo or print keeps PHP from running the script. An array, false or null only spoils the output.
- Parse error: syntax error, unexpected token ",". PHP met a comma where only one value fits. Either one pair of parentheses holds several values, as in
echo("a", "b");, orprintgot more than one value. Remove the parentheses, or changeprinttoecho. - Parse error: syntax error, unexpected token ";". A comma is left after the last value, as in
echo "a", "b",;. Delete that last comma. - Warning: Array to string conversion. The value is an array, which echo can only print as the word Array. Use
print_r()to see what it holds. - Nothing appears where false or null should be. Both turn into empty text when echoed. Check the value with
var_dump()while testing.
This last script hands print two values. Once you've seen its parse error, change print to echo, and the same line prints HelloWorld.
print "Hello", "World";Parse error: syntax error, unexpected token "," in /app/main.php on line 2
Exercise
This echo statement stops with a parse error. Keep the <?php tag and fix the statement so the script prints Name: Ada on the first line and Language: PHP on the second, using echo or print.
echo("Name: Ada", "\n", "Language: PHP", "\n");
Name: Ada Language: PHP
Show the solution
echo "Name: Ada", "\n", "Language: PHP", "\n";
Quiz
This quiz has 5 questions. Pick an answer to see why it is right or wrong.
-
1What does this script print?
echo "[", false, "]";echo turns false into an empty string, so nothing appears between the brackets. The tempting [0] never shows up, because false never becomes a digit when echoed, while true becomes 1.
-
2What does this script print?
echo "B", print "A";echo outputs its values from left to right, so B comes first. Then print outputs A and returns 1, which echo prints last. AB1 would need print to run before echo starts, but echo begins with B.
-
3Which of these lines stops PHP with a parse error?
One pair of parentheses can hold only one value, and "Hello", "World" is two. With each value in its own pair, echo ("Hello"), ("World"); prints HelloWorld.
-
4What does this file print?
<p> "Hi", "!" </p>The short echo tag outputs every comma-separated value with nothing in between, like echo. <p>Hi !</p> would need a space inside one of the strings.
-
5What does the PHP_EOL constant hold?
PHP_EOL is \r\n on Windows and \n on Linux and macOS. It is a line ending, not an HTML tag, and it can't add a <br> to a web page.























