redd 發表於 2017-4-10 15:08:13

echo 和 print

echo
<?php
$txt1="Learn PHP";
$txt2="redd.mx.nf";
$cars=array("Volvo","BMW","SAAB");

echo $txt1;
echo "<br>";
echo "Study PHP at $txt2";
echo "My car is a {$cars}";
?>print
<?php
$txt1="Learn PHP";
$txt2="redd.mx.nf";
$cars=array("Volvo","BMW","SAAB");

print $txt1;
print "<br>";
print "Study PHP at $txt2";
print "My car is a {$cars}";
?>

redd 發表於 2017-4-10 15:11:17

echo - 能够输出一个以上的字符串
print - 只能输出一个字符串,并始终返回 1
echo 可以這樣,但 print 不行:
echo "This", " string", " was", " made", " with multiple parameters.";
提示:echo 比 print 稍快,因为它不返回任何值。

admin 發表於 2020-2-26 18:42:20

$num = 13.9;

printf("數值為:%s", $num);
printf("數值為:%.2f", $num);printf 可以設定更複雜的格式。

redd 發表於 2020-10-15 12:06:10

The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print.
頁: [1]
查看完整版本: echo 和 print