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:如果我调用它,为什么包含的函数总是首先回显?


基础教程推荐
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何替换eregi() 2022-01-01