Home > Article > Backend Development > Example tutorial of echo and print statements in PHP
In PHP, there are two basic output methods: echo
and print
.
The difference between echo and print
echo can output more than one string.
print can only output a 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 structure that can be used with or without parentheses: echo or echo().
Display strings
The following example shows how to use the echo command to display different strings (also please note that the string can contain HTML tag):
<?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 variables
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 (please also note that the string can contain HTML tag):
<?php print "<h2>PHP is fun!</h2>"; print "Hello world!<br>"; print "I'm about to learn PHP!"; ?>
Display variables
The following example shows how to use the print command to display strings and variables:
<?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]}"; ?>
Recommended tutorial: PHP tutorial
The above is the detailed content of Example tutorial of echo and print statements in PHP. For more information, please follow other related articles on the PHP Chinese website!