There are two basic output methods in PHP: echo and print.
In this chapter we will discuss the usage of the two statements in detail and demonstrate how to use echo and print in examples.
The difference between echo and print:
echo - can output one or more strings
print - only allows output of a string, the return value is always 1
Tip: The output speed of echo is faster than that of print. Echo has no return value, while print has a return value of 1.
Echo is a language structure that can be used without parentheses or with parentheses: echo or echo().
display string
The following example demonstrates how to use the echo command to output a string (the string can contain HTML tags):
<?php echo " <h2>PHP is fun!</h2> " ; echo " Hello world!<br> " ; echo " I want to learn PHP!<br> " ; echo " This is a " , " string, " , " using " , " multiple " , " parameters. " ; ?>
display variables
The following example demonstrates how to use the echo command to output variables and strings:
<?php $txt1 = " Learn PHP " ; $txt2 = " codercto.COM " ; $cars = array ( " Volvo " , " BMW " , " Toyota " ) ; echo $txt1 ; echo " <br> " ; echo " Learn PHP at $txt2 " ; echo " <br> " ; echo " The brand of my car is {$cars[0]} " ; ?>
Print is also a language structure that can use parentheses or not: print or print().
display string
The following example demonstrates how to use the print command to output a string (the string can contain HTML tags):
<?php print " <h2>PHP is fun!</h2> " ; print " Hello world!<br> " ; print " I want to learn PHP! " ; ?>
display variables
The following example demonstrates how to use the print command to output variables and strings:
<?php $txt1 = " Learn PHP " ; $txt2 = " codercto.COM " ; $cars = array ( " Volvo " , " BMW " , " Toyota " ) ; print $txt1 ; print " <br> " ; print " Learn PHP at $txt2 " ; print " <br> " ; print " The brand of my car is {$cars[0]} " ; ?>