PHP5:如果我调用它,为什么包含的函数总是首先回显? [英] PHP5: How come an included function always echoes first if I call it?

查看:34
本文介绍了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.phpreturn "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:如果我调用它,为什么包含的函数总是首先回显?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆