How to Print Output in PHP with echo
echo is how a PHP script writes text: to the terminal for a command-line script, to the page for a web request. It prints exactly what you give it, with no spaces or new lines added, so you place those yourself.
Syntax
echo <expression>, <expression>, ...;
| Part | What it does |
|---|---|
echo | Prints one or more values. It's a language construct, so it needs no brackets. |
<expression>, ... | The values, separated by commas, printed one after another. |
"\n" | A new line, in a double-quoted string. |
The PHP manual for echo: "Outputs one or more expressions, with no additional newlines or spaces." And: "echo is not a function but a language construct."
The Example
The example prints a sentence, a label with a number, and one line built from two echo statements.
Prerequisites
- PHP for the command line. This guide uses PHP 8.6.0.
Step 1: Write the Code
Create a file named main.php:

<?php
echo "Hello from PHP\n";
echo "Total: ", 42, "\n";
echo 'No new line here, ';
echo "then a new line.\n";
Step 2: Run It
php main.php
Hello from PHP
Total: 42
No new line here, then a new line.

"Total: ", 42, "\n"printed three values one after another, with no spaces added between them.- The third
echoended without"\n", so the fourth continued on the same line.
The command is the same in zsh, bash and PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing echo("a", "b") with brackets around several values | Parse error: syntax error, unexpected token "," | Leave out the brackets: echo "a", "b"; |
Writing '\n' in single quotes | It prints a backslash and an n, not a new line | Use "\n" in double quotes |
| Echoing an array | Warning: Array to string conversion, then the word Array | Use print_r or var_dump for arrays |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Output runs together on one line | No "\n" at the end | Add "\n" |
\n appears literally | The string is in single quotes | Switch to double quotes |
FAQ
What's the difference between echo and print?
print takes one value and returns 1, so it can be used inside an expression; echo takes several values and returns nothing. For printing, they work the same.
Where does echo output go?
In a command-line script, to standard output; under a web server, into the page sent to the browser.
Conclusion
echo prints its values exactly as given, separated by nothing and followed by nothing. Separate several values with commas and end lines with "\n".
Additional Resources
- echo, in the PHP manual.
