echo & print
echo and print are the two language constructs PHP gives you for sending text to the output buffer — the HTML response, a CLI terminal, wherever the script's output is headed. They look interchangeable in the simplest cases, which is exactly why people assume they're identical. They aren't quite: they differ in whether they return a value, whether they accept multiple arguments, and (in practice, negligibly) in raw speed. Knowing the difference means you'll pick the right one on purpose instead of by habit.
The basics
Simplest form of each
<?php echo "Hello, world!"; print "Hello, world!"; // Both produce identical output for a single string.
Hello, world!Hello, world!
echo accepts multiple, comma-separated arguments
echo is a language construct, not a function, and one of its quirks is that it can take several expressions in one call, printing them back to back with no separator inserted.
Multiple arguments to echo
<?php $name = 'Ada'; $role = 'engineer'; echo 'Name: ', $name, ', Role: ', $role, PHP_EOL;
Name: Ada, Role: engineer
print, by contrast, only ever accepts a single argument. Trying to pass it a comma-separated list is a syntax error — what looks like "multiple arguments" to print is actually just the comma operator, which PHP doesn't support the way some languages do.
This does NOT work
<?php print 'Name: ', $name; // Parse error: syntax error, unexpected ','
print returns a value; echo does not
This is the most consequential difference. print is an expression that always evaluates to the integer 1, which means it can be used anywhere an expression is expected — inside a ternary, as an operand in a boolean expression, or chained with the and/or operators. echo, on the other hand, has no return value at all; it cannot be used inside another expression.
print's return value in a real expression
<?php
$loggedIn = true;
// print's return value (1) lets it live inside a boolean expression
$loggedIn and print('Welcome back!' . PHP_EOL);
// Capturing the return value directly (rarely useful, but it exists)
$result = print 'Saving...';
echo PHP_EOL . "print() returned: $result";Welcome back! Saving... print() returned: 1
echo cannot be used as an expression
<?php $x = echo 'test'; // Parse error: syntax error, unexpected token "echo"
The short echo tag
Inside HTML-heavy templates, wrapping every dynamic value in a full <?php echo ...; ?> block gets noisy fast. PHP provides a shorthand, <?= ?>, which is exact sugar for <?php echo ?> — including the fact that it can take echo's comma-separated argument list.
Short echo tags in a template
<?php $user = ['name' => 'Grace', 'unread' => 4]; ?> <h2>Welcome, <?= $user['name'] ?></h2> <p>You have <?= $user['unread'] ?> unread messages.</p> <!-- Equivalent to writing <?php echo $user['name']; ?> etc. -->
Performance: does it actually matter?
Benchmarks consistently show echo as marginally faster than print — because print has to set up and return a value that echo never bothers computing. In practice this difference is a few nanoseconds per call and will never be the bottleneck in a real application; a single database query costs orders of magnitude more than the choice between these two constructs. Treat the performance angle as trivia, not as a reason to refactor.
When to reach for which
Situation | Prefer |
|---|---|
Printing several values in one call |
|
Templating inside HTML markup |
|
Need the output call to act as an expression (ternaries, |
|
General-purpose "just print this string" |
|
echoandprintare language constructs, not functions — parentheses around their argument are optional and purely stylistic.echocan take multiple comma-separated arguments;printtakes exactly one.printalways evaluates to1and can be used inside expressions;echohas no value and cannot.<?= $expr ?>is identical to<?php echo $expr; ?>.The speed difference between them is real but practically irrelevant.