输出语句

  • echo - 可以输出一个或多个字符串
  • print - 只能输出简单类型变量的值,如int,string
  • print_r - 可以输出复杂类型变量的值,如数组,对象
  • sprintf - 将输出的数据保存到变量

echo

$txt1="Hello ";
$txt2="world";
$cars=array("Volvo","BMW","Toyota");
echo $txt1."<br>"; // Hello
echo "Study PHP at \$txt2<br>"; // Study PHP at $txt2
echo "Study PHP at $txt2<br>"; // Study PHP at world
echo "My car is a $cars[0] <br>"; // My car is a Volvo
echo "My car is a {$cars[0]}<br>"; // My car is a Volvo

print

$txt1="Hello ";
$txt2="world";
$cars=array("Volvo","BMW","Toyota");
print "<h2>PHP is fun!</h2>"; // PHP is fun!
print "Hello world!<br>"; // Hello world!
print "I'm about to learn PHP!<br>"; // I'm about to learn PHP!
print $txt1.$txt2."<br>"; // Hello world
print $cars[2]; // BMW

print_r

$txt1="Hello World!";
$cars=array("Volvo","BMW","Toyota");
print_r($txt1); echo "<br>"; // Hello World!
print_r($cars); echo "<br>"; // Array ( [0] => Volvo [1] => BMW [2] => Toyota )

sprintf

$txt = sprintf("Hello world!");
echo $txt; // Hello world!

MIT Licensed | Copyright © 2018-present 滇ICP备16006294号

Design by Quanzaiyu | Power by VuePress