如果我在这段代码中使用print
:
<?php
for($i = 1; $i <= 3; print $i . "\n") {
$i++;
}
?>
我看到这样的输出:
2
3
4
但是当我使用 echo
时代码不起作用:
<?php
for($i = 1; $i <= 3; echo $i . "\n") {
$i++;
}
?>
我看到这个错误:
PHP Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ')' in /media/datos/xampp/htdocs/temp/1.php on line 3
我的问题是:
- 为什么我可以将
print
用作 for
循环中的第三个表达式,但在使用 echo
时却不能,为什么它们的行为与彼此?
引用资料:
Expression. print() behaves like a function in that you can do: $ret
= print "Hello World"; And $ret will be 1. That means that print can be used as part of a more complex expression where echo cannot. An
example from the PHP Manual:
$b ? print "true" : print "false";
我的部分回答是以下回答的一部分。我想这就是你问题的答案。最重要的部分是 print()
表现得像一个函数
查看此答案:https://stackoverflow.com/a/234255/1848929
echo
怎么样:
Note: Because this is a language construct and not a function, it
cannot be called using variable functions.
请参阅本页注释部分:http://us2.php.net/manual/en/function.echo.php