Summary:
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.
1. Two common output statements of PHP
The difference between echo and print:
echo - can output one or more strings
print - only one string is allowed to be output, and the return value is always 1
Note: echo outputs faster than print, echo has no return value, and print has a return value of 1. In most cases, echo is still used for output.
2. PHP echo statement
echo is a language structure, which 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 characters String (string can contain HTML tags):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php echo "<h1>大家都来进入PHP的世界吧!</h1>"; echo "Hello world!<br>"; echo "我"," 想"," 学习 "," PHP!<br>"; ?> </body> </html>
Display variable
The following example demonstrates how to use The echo command outputs variables and strings:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php $a="Hello world!"; $b="欢迎来到php"; echo $a; echo "<br/>"; echo $b; ?> </body> </html>
3. PHP print statement
print is also a language structure, with or without parentheses: print or print().
Display string
The following example demonstrates how to use the print command to output a string (the string can contain HTML tag):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php print "<h1>大家都来进入PHP的世界吧!</h1>"; print "Hello world!<br>"; ?> </body> </html>
Display variables
The following example demonstrates how to use the print command to output variables and characters String:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> </head> <body> <?php $a="Hello world!"; $b="欢迎来到php"; print $a; print "<br/>"; print $b; ?> </body> </html>
Learning experience: Note that echo can output one or more strings, while print can only output one string, and the return value is always 1.