今天,
当我们引用一本书时,我们发现 echo 有多个参数,但几乎没有测试,它给了我们奇怪的行为。
这是一个测试:
echo("one", "two");
还有一个:
echo "one", "two";
第一个测试用例给出了错误,但第二个没有..为什么?
最佳答案
文档给了你答案:
echo is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses.
http://php.net/manual/en/function.echo.php
所以,简而言之:因为 echo
不是函数 - 跳过括号。如果只想回显一个参数,则可以使用括号,但通常不这样做。
<?php
echo 'Hello'; //Acceptable
echo('Hello'); //Acceptable
echo 'Hello ', 'world'; //Acceptable
echo ('Hello ', 'world'); //Not acceptable
?>
关于php - 为什么 echo 行为不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31944136/