PHP5: How come an included function always echoes first if I call it?(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.
这篇关于PHP5:如果我调用它,为什么包含的函数总是首先回显?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP5:如果我调用它,为什么包含的函数总是首先回显?
- 带有通配符的 Laravel 验证器 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Laravel 仓库 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01