PHP5:如果我调用它,为什么包含的函数总是首先回显?
我这里有两个文件:
ToBeIncludedFile.php
ToBeIncludedFile.php
<?php
function printOut(){
echo "World!";
}
?>
MainFile.php
MainFile.php
<?php
include("ToBeIncludedFile.php");
echo "Hello ".printOut();
?>
我会期待Hello World!".相反,我得到了这个:世界!你好".
I would expect "Hello World!". Instead I get this: "World!Hello ".
我知道如果我写return而不是echo,那么一切都很好.是因为我正在回显一个已经回显字符串的函数吗?但是为什么它会打印出字符串World!"先不报错?
I know if I write return instead of echo, then everything is fine. Is it because I'm echoing a function that is already echoing a string? But then why would it print out the string "World!" first and not throw an error?
推荐答案
之所以先回显,是因为它被调用,而之后是字符串连接"(更多内容在第二):
The reason it echos first, is because it is called, and afterwards are the strings "concatenated" (more on that in a second):
你想要的 ToBeIncludedFile.php
是 return "World!";
,而不是 echo.
What you want in ToBeIncludedFile.php
is return "World!";
, not echo.
现在是这样的:
- 您包含文件,它不打印任何内容,这是正确的.
- 您将字符串Hello"和
printOut()
的返回值 串联起来.这意味着,首先调用该函数: - printOut() 执行并打印World!",不返回任何内容.
- 然后,您的主脚本将Hello"与任何内容连接起来并打印出来.
- You include the file, which doesn't print anything, this is correct.
- You do a concatenation of the string "Hello" and the return value of
printOut()
. That means, first that function is called: - printOut() executes and prints "World!", returning nothing.
- Your main script then concatenates "Hello" with nothing and prints that.
相关文章