03 – PHP echo and print statements

In PHP the echo and print statements are two basic ways of getting output.

They both output data on the screen but there are three small differences between them.

  • The echo statement has no return value while the print statement has a return value of 1, so it can be used in expressions.
  • The echo statement in PHP is a little bit faster than the print statement.
  • The echo can take multiple parameters while the print statement can take only one.

Note:
Both, echo and print statements in PHP can be used with or without parenthesis. So, echo and echo() are the same; print and print() are the same.

The echo statement in PHP

In PHP, the echo statement can output text, variables and HTML markup.

<?php

echo "<h3>I am learning PHP!</h3>";
echo "Hi there!<br>";
echo "PHP is cool!<br>";
echo "Multiple ", "parameters ", "output.", "<br>", "<br>";

$a = 3;
$b = 4;
echo "Number " . $a . " added to number " . $b . " equals to: ";
echo $a + $b;

?>

The print statement in PHP

Here is the same example as above but using the print statement instead of echo statement.

<?php

print "<h3>I am learning PHP!</h3>";
print "Hi there!<br>";
print "PHP is cool!<br>";
print "The print statement takes only one argument!<br><br>";
// print "Multiple ", "parameters ", "output.", "<br>", "<br>";

$a = 3;
$b = 4;
print "Number " . $a . " added to number " . $b . " equals to: ";
print $a + $b;

?>

Leave a Reply