Learn

Short lessons with real code you can edit and run on the page.

echo and print in PHP

Lesson 2 PHP 8.5 Runs in your browser
In short

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

  • echo accepts several comma-separated values, but print "a", "b"; is a parse error because print takes one.
  • print always gives back the value 1, which is why echo print "Hello"; outputs Hello1.
  • The truth value true echoes as 1, while false and null (no value) echo nothing.
  • PHP_EOL is "\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 a Warning: Array to string conversion where 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.

PHP
<?php
echo "Hello", ", ", "World", "!\n";
echo "Total: ", 2 + 3, "\n";
Output
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.

PHP echo vs print
Featureechoprint
How many valuesOne or more, with commasExactly one
ReturnsNothingAlways 1
Usable as a valueNo, a parse errorYes
Short tag<?=None
Wrapped in ( )Yes, one value per pairYes
PHP
<?php
echo print "Hello", "\n";
Output
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").

PHP
<?php
echo("Hello"), "\n";
echo(2 + 3) * 10, "\n";
Output
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.

PHP
<?php
echo("Hello", "World");
Output

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.

Ways to start a new line in PHP output
Where the output goesHow 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 PHPPHP_EOL, which matches that system's line ending
A web pageA <br> tag, because a browser shows \n as a space
A web page, for text that already has line breaksnl2br(), which adds <br /> before each line break
A web page, with the breaks kept as typedA <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.

PHP
<?php
echo nl2br("One\nTwo");
Output
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.

PHP
<?php
echo "Monday
Tuesday
";
echo "Wednesday\n", "Thursday\n";
Output
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.

PHP
<?php
echo "Line one", PHP_EOL, "Line two", PHP_EOL;
Output
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.

PHP
<?php
echo strlen(PHP_EOL);
Output
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.

PHP
<?php
echo "<h1>Welcome</h1>\n";
echo '<p class="intro">First line<br>Second line</p>', "\n";
Output
<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.

PHP
<ul>
<li><?= "echo", " and ", "print" ?></li>
<li>Total: <?= 2 + 3 ?></li>
</ul>
Output
<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.

PHP
<?php
echo "[", true, "]\n";
echo "[", false, "]\n";
echo "[", null, "]\n";
Output
[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.

PHP
<?php
echo ["apple", "banana"];
Output

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");, or print got more than one value. Remove the parentheses, or change print to echo.
  • 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.

PHP
<?php
print "Hello", "World";
Output

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.

PHP
<?php
echo("Name: Ada", "\n", "Language: PHP", "\n");
Output
Show the solution
<?php
echo "Name: Ada", "\n", "Language: PHP", "\n";

Quiz

This quiz has 5 questions. Pick an answer to see why it is right or wrong.

  1. 1What does this script print?

    <?php
    echo "[", false, "]";
  2. 2What does this script print?

    <?php
    echo "B", print "A";
  3. 3Which of these lines stops PHP with a parse error?

  4. 4What does this file print?

    <p><?= "Hi", "!" ?></p>
  5. 5What does the PHP_EOL constant hold?

Frequently asked questions

Should you use echo or print in PHP?
Use echo unless you need output inside an expression. echo can print several comma-separated values in one statement, while print takes one value and returns 1, which only matters where PHP expects a value.
Why does echo false print nothing in PHP?
PHP converts false to an empty string before echo outputs it. null also becomes an empty string, while true becomes 1. To see the real value while testing, var_dump(false) prints bool(false).
Why is my PHP new line not working?
Check the quotes first, because \n inside single quotes prints as a backslash and the letter n. Use double quotes for \n. In ordinary web page text, a browser shows line breaks as spaces, so add a <br> tag or use nl2br() there.
What is the shorthand for echo in PHP?
The short echo tag <?= is the shorthand for <?php echo. It accepts several values separated by commas, and the closing ?> tag ends the statement, so no semicolon is needed. It keeps working on servers where short tags are turned off.
Is PHP_EOL better than \n in PHP?
Neither PHP_EOL nor \n is better everywhere. PHP_EOL is the line ending of the system running PHP, so it suits files and logs read on that system, while \n is always one line feed. In ordinary web page text the choice makes no difference, because a browser shows both as a space.
How do you echo a variable in PHP?
Write the variable after echo, outside single quotes. For a variable named $name, echo $name; outputs the value it holds. With commas, echo 'Hi, ', $name; prints Hi, Ada when $name holds the text Ada.