W3docs

Understanding PHP echo and print statements

In the world of PHP programming, the two most commonly used methods for outputting text are the echo and print statements. These two functions perform similar

Outputting text is the first thing most PHP programs do, and PHP gives you two tools for it: echo and print. They look interchangeable, and for everyday output they are — but they are not the same thing under the hood. Both are language constructs (not functions in the strict sense), so you can call them with or without parentheses.

This chapter shows how each one works, where they differ, and which to reach for. If you are new to outputting values, it helps to first read about PHP variables and PHP strings, since echo and print are how you display them.

PHP echo statement

The echo statement outputs one or more strings. Its most common use is to print a single piece of text:

PHP example of echo

php— editable, runs on the server

echo does not return a value. The string above goes straight to the output buffer and is sent to the browser (or the terminal, for CLI scripts).

Outputting variables and expressions

You can print variables directly, or build up a longer string with the . concatenation operator:

PHP echo concatenated string

php— editable, runs on the server

Inside double-quoted strings, variables are interpolated for you, so you often don't even need concatenation:

<?php

$num = 5;
echo "The value of num is: $num";   // The value of num is: 5
echo 'The value of num is: $num';   // The value of num is: $num

Single quotes are taken literally — $num is printed as-is. See PHP strings for the full rules.

Passing multiple arguments

A feature print does not have: echo accepts several comma-separated arguments and prints them in order. This can be slightly faster than concatenating because PHP doesn't build one big string first:

<?php

echo "Red", " ", "Green", " ", "Blue", "\n";
// Output: Red Green Blue

Note: the comma form only works when you call echo without parentheses. echo("a", "b") is a syntax error.

The short echo tag

When you mix PHP into HTML, <?= ... ?> is shorthand for <?php echo ... ?> — handy in templates:

<p>Welcome, <?= $name ?>!</p>

PHP print statement

print also outputs text, but with one important twist: it always returns the value 1. That return value lets you use print inside expressions, where echo would be a syntax error:

PHP print function example

php— editable, runs on the server

Because it returns 1, print is sometimes used in conditional one-liners like $ok or print "failed";. In practice this is rare — most code just uses it like echo.

Like echo, print handles variables and expressions:

PHP print concatenated string

php— editable, runs on the server

The one thing to remember: print takes exactly one argument. You cannot write print "a", "b";.

Comparison between echo and print

Featureechoprint
Returns a valueNoAlways returns 1
Usable in an expressionNoYes ($x = print "hi";)
Multiple argumentsYes (echo $a, $b;)No — one argument only
SpeedMarginally fasterMarginally slower

A few practical takeaways:

  • The speed difference is negligible for real applications — it's a micro-optimization you'll almost never notice. Choose based on readability, not performance.
  • Use echo for normal output. It is the conventional choice and supports multiple arguments.
  • Reach for print only when you genuinely need its return value inside an expression.

Conclusion

echo and print are both language constructs for sending text to the output. Use echo by default — it is the idiomatic choice and can take multiple arguments — and use print when you specifically need a return value. To go further, see PHP variables, PHP strings, and PHP data types.

Practice

Practice
What is the difference between echo and print in PHP?
What is the difference between echo and print in PHP?
Was this page helpful?