echo vs print StatementsBoth echo and print are language constructs (not functions) used to output text, HTML, and data variables to the response stream in PHP.
echo and print| Feature | echo |
print |
|---|---|---|
| Return Value | No return value (void) |
Always returns 1 (can be used in expressions) |
| Arguments | Accepts multiple arguments separated by commas | Accepts only a single argument |
| Speed / Performance | Marginally faster | Slightly slower |
| Template Syntax | <?= $var ?> shorthand available |
No short tag equivalent |
flowchart LR
A["PHP Script Output"] --> B{"Choose Output Construct"}
B -- Multiple Strings / Short Echo --> C["echo $str1, $str2;"]
B -- Expression Context Needed --> D["print($str); returns 1"]
C --> E["HTTP Output Stream"]
D --> E
<?php
declare(strict_types=1);
$siteTitle = "Kodersolution";
$articleCount = 42;
// 1. Standard echo output
echo "Welcome to " . $siteTitle . "!
";
// 2. Echo with multiple comma-separated arguments (faster than concatenation)
echo "Platform: ", $siteTitle, " | Published Articles: ", $articleCount, "
";
// 3. Print construct in boolean expression context
$printedSuccessfully = print("Rendering Header Banner...
");
echo "Print Returned Status Code: " . $printedSuccessfully . "
";
// 4. Interpolated double-quoted string output
echo "System Status: $articleCount active articles on $siteTitle.
";
?>
<!-- 5. Short Echo Tag shorthand in HTML templates -->
<div>
<h1><?= htmlspecialchars($siteTitle) ?></h1>
<p>Articles Count: <?= $articleCount ?></p>
</div>
echo and <?= ?> in Web Templates: echo is the industry standard for PHP view rendering.echo $a, $b, $c; avoids creating temporary concatenated string buffers in memory.'Literal String' when no variable replacement is needed for slight parsing performance gains.Write an echo statement that passes 3 separate comma-separated strings without using string concatenation (.)!
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.
You've completed this section! Take a quick 5-question quiz to check your understanding.