PHP 5 echo and print statements

In PHP, there are two basic methods of output: echo and print.

In this tutorial, we use echo and print almost in every example. Therefore, this section explains more about these two output statements.

PHP echo and print statements

Differences between echo and print:

  • echo - Can output more than one string
  • print - Can only output a single string and always returns 1

Tip:echo is slightly faster than print because it does not return any value.

PHP echo statement

echo is a language construct that can be used with or without parentheses: echo or echo().

Display Strings

The following example demonstrates how to use the echo command to display different strings (please note that strings can contain HTML tags):

<?php
echo "<h2>PHP is fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This", " string", " was", " made", " with multiple parameters.";
?>

Run Example

Display Variables

The following examples demonstrate how to use the echo command to display strings and variables:

<?php
$txt1="Learn PHP";
$txt2="codew3c.com";
$cars=array("Volvo","BMW","SAAB");
echo $txt1;
echo "<br>";
echo "Study PHP at $txt2";
echo "My car is a {$cars[0]}";
?>

Run Example

PHP print Statement

print is also a language construct and can be used with or without parentheses: print or print().

Display Strings

The following examples demonstrate how to use the print command to display different strings (note that strings can contain HTML tags):

<?php
print "<h2>PHP is fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>

Run Example

Display Variables

The following examples demonstrate how to use the print command to display strings and variables:

<?php
$txt1="Learn PHP";
$txt2="codew3c.com";
$cars=array("Volvo","BMW","SAAB");
print $txt1;
print "<br>";
print "Study PHP at $txt2";
print "My car is a {$cars[0]}";
?>

Run Example