编程语言
917
1. echo
echo 是一个语言结构,有无括号均可使用:echo 或 echo()。
显示字符串
下面的例子展示如何用 echo 命令来显示不同的字符串(同时请注意字符串中能包含 HTML 标记):
<?php echo "<h2>PHP is enjoyable!</h2>"; echo "Hello world!<br>"; echo "I'm about to learn PHP!<br>"; echo "This", " string", " was", " made", " with multiple parameters."; ?>
输出结果:
PHP is enjoyable! Hello world! I'm about to learn PHP! This string was made with multiple parameters.
显示变量
下面的例子展示如何用 echo 命令来显示字符串和变量:
<?php $text1 = "Learn PHP"; $text2 = "testtest"; $vehicles = array("Volvo", "BMW", "SAAB"); echo $text1; echo "<br>"; echo "Study PHP at $text2"; echo "<br>"; echo "My car is a {$vehicles[0]}"; ?>
输出结果:
Learn PHP testtest My car is a Volvo
2. PHP print 语句
print 也是语言结构,有无括号均可使用:print 或 print()。
显示字符串
下面的例子展示如何用 print 命令来显示不同的字符串(同时请注意字符串中能包含 HTML 标记):
<?php print "<h2>PHP is enjoyable!</h2>"; print "Hello world!<br>"; print "I'm about to learn PHP!"; ?>
输出结果:
PHP is enjoyable! Hello world! I'm about to learn PHP!
显示变量
下面的例子展示如何用 print 命令来显示字符串和变量:
<?php $text1 = "Learn PHP"; $text2 = "Example.com"; $vehicles = array("Volvo", "BMW", "SAAB"); print $text1; print "<br>"; print "Study PHP at $text2"; print "<br>"; print "My car is a {$vehicles[0]}"; ?>
输出结果:
Learn PHP Study PHP at Example.com My car is a Volvo
3. echo print的差异
- echo - 能够输出一个以上的字符串
- print - 只能输出一个字符串,并始终返回 1
由于echo不返回任何值, 所以 echo比print稍快