PHP7 echo and print statement instance usage

  • 2021-11-24 01:05:21
  • OfStack

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

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

PHP echo and print Statements

Differences between echo and print:

echo-Able to output more than 1 string print-Only 1 string can be output and 1 is always returned

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

PHP echo Statement

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

Display string

The following example shows how to use the echo command to display different strings (also 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.";
?>

Display variable

The following example shows how to use the echo command to display strings and variables:


<?php
$txt1="Learn PHP";
$txt2="W3School.com.cn";
$cars=array("Volvo","BMW","SAAB");

echo $txt1;
echo "<br>";
echo "Study PHP at $txt2";
echo "My car is a {$cars[0]}";
?>

PHP print statement

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

Display string

The following example shows how to use the print command to display different strings (also 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!";
?>

Display variable

The following example shows how to display strings and variables with the print command:


<?php
$txt1="Learn PHP";
$txt2="W3School.com.cn";
$cars=array("Volvo","BMW","SAAB");

print $txt1;
print "<br>";
print "Study PHP at $txt2";
print "My car is a {$cars[0]}";
?>


Related articles: