php基础语法——输出语句

三、输出语句:echo 和 print

1)echo 和 print 之间的差异:

  • echo - 能够输出一个以上的字符串
  • print - 只能输出一个字符串,并始终返回 1
  • echo 比 print 稍快,因为它不返回任何值。

2) echo 语句

echo 是一个语言结构,有无括号均可使用:echo 或 echo()。

下面的例子展示如何用 echo 命令来显示不同的字符串(同时请注意字符串中能包含 HTML 标记):

实例:

<?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.";
?>

下面的例子展示如何用 echo 命令来显示字符串和变量:

实例:

<?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]}";
?>

3) print 语句

print 也是语言结构,有无括号均可使用:print 或 print()。

下面的例子展示如何用 print 命令来显示不同的字符串(同时请注意字符串中能包含 HTML 标记):

实例:

<?php
print "<h2>PHP is fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>

下面的例子展示如何用 print 命令来显示字符串和变量:

实例:

<?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]}";
?>

猜你喜欢

转载自blog.csdn.net/qq_42133100/article/details/96864577