Skip to content

PHP Echo / Print

PHP provides two primary language constructs to send output (like text, HTML, or variable values) to the browser: echo and print.

While often used interchangeably, especially in tutorials, understanding their subtle differences is useful.

echo and print are very similar language constructs used for outputting data. Their key differences are:

  • Return Value: echo does not have a return value (it’s purely a language construct). print always returns 1, allowing it to be used within expressions (though this is rarely practical or recommended).
  • Parameters: echo can accept multiple arguments separated by commas (e.g., echo $var1, $var2, " text";), although this syntax is uncommon. print can only accept one argument.
  • Performance: echo is generally considered marginally faster than print because it doesn’t have a return value. However, this difference is negligible in almost all real-world applications.
  • Syntax: Both can be used with or without parentheses around their arguments, as they are language constructs, not functions (e.g., echo "Hello"; or echo("Hello");). Parentheses are more common when used in complex expressions (more likely with print, if used that way).

Recommendation: Most PHP developers prefer echo due to its slightly simpler nature (no return value) and common usage convention. Consistency is key; choose one and stick with it within your project.

echo is used to output strings, numbers, HTML markup, or the values of variables.

<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
// Using multiple arguments (less common)
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>

You can embed variables directly in double-quoted strings or concatenate using the dot (.) operator.

<?php
$courseTitle = "Learn PHP";
$websiteName = "ExamplePlatform.com";
$num1 = 5;
$num2 = 4;
// Embedding variables in double quotes
echo "<h2>$courseTitle</h2>";
echo "Study PHP at $websiteName<br>";
// Concatenating strings and variables
echo "The sum of " . $num1 . " and " . $num2 . " is: " . ($num1 + $num2);
?>

print functions similarly to echo but only takes one argument and returns 1.

<?php
print "<h2>PHP is Fun! (using print)</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
// Using print in an expression (rarely useful)
$isPrinted = print "<br>This was printed."; // Outputs the text and $isPrinted becomes 1
?>

Similar to echo, you can embed variables in double-quoted strings or use concatenation.

<?php
$courseTitle = "Learn PHP";
$websiteName = "ExamplePlatform.com";
$num1 = 5;
$num2 = 4;
// Embedding variables
print "<h2>$courseTitle</h2>";
print "Study PHP at $websiteName<br>";
// Concatenation
print "The sum is: " . ($num1 + $num2);
?>

For general output, echo is the standard choice in the PHP community. Use print only if you have a specific, albeit uncommon, reason to leverage its return value within an expression.

For more complex formatting, consider functions like printf or sprintf. For outputting within HTML templates, short echo tags (<?= $variable ?>) can be convenient (ensure short_open_tag is enabled or use PHP 7.0+ where it’s always available regardless of the setting).