The two basic output methods of php are "echo" and "print". Echo is used to output one or more strings and can accept multiple parameters and has no return value. The syntax is "echo($str)"; while print is used to output a string and can only accept one parameter and has a return value. The syntax is "echo($str)". "print($str)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, There are two basic output methods: echo
and print
.
Difference between echo and print
Tip: echo is slightly faster than print because It returns no value.
1.echo is a language structure and can be used with or without parentheses: echo or echo();
2. Display string , the following example shows how to use the echo command to display different strings (please also note that the string can contain HTML tags)
<?php echo "<h2>PHP is fun!</h2>"; echo(123); echo "<br>"; echo("我爱php"); echo "<br>"; echo "Hello world!<br>"; echo "I'm about to learn PHP!<br>"; echo "This", " string", " was", " made", " with multiple parameters."; ?>
Effect:
PHP is fun! 123 我爱php Hello world! I'm about to learn PHP! This string was made with multiple parameters.
3.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 <br />"; echo "My car is a {$cars[0]}"; ?>
Effect:
Learn PHP Study PHP at W3School.com.cn My car is a Volvo
1. Print is also a language structure and can be used with or without parentheses: print or print().
2. Display strings. The following example shows how to use the print command to display different strings (please note that the string can contain HTML tags):
<?php print "<h2>PHP is fun!</h2>"; print "Hello world!<br>"; print "I'm about to learn PHP!"; ?>
Effect:
PHP is fun! Hello world! I'm about to learn PHP!
3. 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 <br>"; print "My car is a {$cars[0]}"; ?>
Effect:
Learn PHP Study PHP at W3School.com.cn My car is a Volvo
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of What are the two basic output methods in php. For more information, please follow other related articles on the PHP Chinese website!