echo and print are the two core language constructs for sending text, numbers, and HTML to the browser or terminal in PHP. Both work without parentheses, though echo is used almost everywhere because it accepts multiple arguments and returns nothing.
echo and printWhile both constructs output strings, their signatures differ slightly under the hood.
| Feature | echo |
print |
|---|---|---|
| Return Value | None (void) |
Always returns 1 |
| Multiple Arguments | Supported (echo $a, $b;) |
Single argument only |
| Expression Use | Cannot be used inside expressions | Can be used inside expressions |
| Template Shortcut | <?= $value ?> |
No dedicated shortcut |
Because echo has no return value overhead and supports comma-separated values, developers favor it for CLI scripts and template output. Using commas instead of string concatenation with . also avoids allocating an intermediate joined string in memory.
<?php
declare(strict_types=1);
$productName = "Mechanical Keyboard";
$price = 89.00;
$inStock = true;
echo "Product: ", $productName, " | Price: $", number_format($price, 2), PHP_EOL;
print "Status: " . ($inStock ? "Available" : "Sold Out") . PHP_EOL;
<?= ?>When generating HTML responses, wrap any user-supplied text with htmlspecialchars() before echoing it. This converts < and > characters into safe HTML entities.
<?php
$username = "<script>alert('xss')</script>Samir";
$safeName = htmlspecialchars($username, ENT_QUOTES, 'UTF-8');
?>
<div class="user-badge">
<p>Signed in as: <strong><?= $safeName ?></strong></p>
</div>
print: Writing print $a, $b; throws a ParseError. Only echo accepts comma-separated arguments.echo [1, 2, 3]; triggers an "Array to string conversion" warning. Use print_r(), var_dump(), or implode().ENT_QUOTES with htmlspecialchars(): Always include ENT_QUOTES and 'UTF-8' when escaping values placed inside HTML attributes.echo a function in PHP?No. Both echo and print are language constructs, not functions, which means parentheses around the argument are optional.
echo or print?echo is marginally faster because it does not return an integer 1, though in real web apps the difference is negligible.
Use var_dump($data) to inspect types and values, or json_encode($data, JSON_PRETTY_PRINT) for clean readable output.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.