PHP – Echo/Print
Compare PHP's echo and print output constructs. Understand their differences and when to use each in your PHP programs.
Table of Contents
PHP: Echo and Print
To successfully display data on a web page utilizing PHP, developers actively rely on two core basic statements: echo and print.
While both serve nearly identical purposes—sending generated outputs directly into the browser’s DOM—understanding the subtle nuances and syntactical differences between them provides you a superior edge.
1. The echo Statement
echo is arguably the most recognizable keyword in all of PHP.
- Return Value: It has absolutely no return value.
- Parameters: It can accept multiple parameters (separated by commas).
- Performance: Because it doesn’t return anything, it is marginally faster than
print. - Syntax: Can be written natively with or without parentheses:
echoorecho().
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
// Echoing multiple parameters natively
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
Displaying Variables with echo
You can seamlessly output text blocks infused deeply with variable states.
<?php
$bookName = "Mastering PHP Web Systems";
$pages = 450;
echo "The book $bookName has $pages pages.";
?>
2. The print Statement
The print statement functions almost identically, with strict mathematical constraints.
- Return Value: It rigidly boasts a return value of 1. This guarantees it can be aggressively utilized deep inside complex expressions (like ternary operations).
- Parameters: It natively accepts exactly one argument.
- Performance: Slightly slower due to computing the return value.
- Syntax: written flawlessly natively with or without parentheses:
printorprint().
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
// Note: Attempting to pass multiple strings separated by commas
// will cause a fatal syntax breakdown!
?>
Short Echo Tag: The Modern Approach
If you solely want a massive block of HTML to receive a single, fast variable, modern server configurations universally advocate utilizing the highly praised Short Echo Tag: <?= ... ?>.
This acts as an explicit shorthand for <?php echo ... ?>.
<?php $user = "CodeMaster99"; ?>
<!-- Instead of this: -->
<h1>Welcome, <?php echo $user; ?>!</h1>
<!-- You write this natively: -->
<h1>Welcome, <?= $user ?>!</h1>
Interactive Sandbox
You can observe exactly how both keywords safely populate the browser viewport. Note how echo brilliantly handles comma separations while print requires strict string concatenation format using the period ..