PHP echo 和 PHP return 之间有什么区别?
是的,我已经用谷歌搜索了这个问题,甚至参考了我的教科书(Don Gosselin 的 PHP),但我似乎真的无法理解这个解释。
据我了解:
echo = 显示函数的最终结果
return = 从函数返回值
我在以下函数中应用了 echo
和 return
我看不出区别或使用 return
而不是 echo
的“有效性”。
<?php
echo "<h1 style='font-family:Helvetica; color:red'>Using <em>echo</em></h1>";
function add1($x, $y){
$total = $x + $y;
echo $total;
}
echo "<p>2 + 2 = ", add1(2, 2), "</p>";
echo "<h1 style='font-family:Helvetica; color:red'>Using <em>return</em></h1>";
function add2($x, $y){
$total = $x + $y;
return $total;
}
echo "<p>2 + 2 = ", add2(2, 2), "</p>";
?>
两者都显示结果! 我不明白什么?
Yes, I have googled this question and even referred to my textbook (PHP by Don Gosselin) but I seriously can't seem to understand the explanation.
From my understanding:
echo = shows the final result of a function
return = returns the value from a function
I applied both echo
and return
in the following functions I can't see the difference or the 'effectiveness' of using return
instead of echo
.
<?php
echo "<h1 style='font-family:Helvetica; color:red'>Using <em>echo</em></h1>";
function add1($x, $y){
$total = $x + $y;
echo $total;
}
echo "<p>2 + 2 = ", add1(2, 2), "</p>";
echo "<h1 style='font-family:Helvetica; color:red'>Using <em>return</em></h1>";
function add2($x, $y){
$total = $x + $y;
return $total;
}
echo "<p>2 + 2 = ", add2(2, 2), "</p>";
?>
Both display the result!
What am I not understanding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我将对此给出一个完全非技术性的答案。
假设有一个名叫莎莉·函数的女孩。你想知道她是否喜欢你。因此,由于您正在上小学,因此您决定向 Sally 传递一张便条(使用参数调用该函数)询问她是否喜欢您。现在你打算做的就是问她这个问题,然后告诉每个人她告诉你的事情。相反,你问她,然后她告诉所有人。这相当于返回(你获取信息并用它做一些事情)与她的回声(在你没有任何控制的情况下告诉每个人)。
在你的情况下,发生的情况是,当 Sally
echo
时,她正在从你手中夺走控制权,并说“我现在要告诉人们这一点”,而不是你能够接受她的回应并做你想做的事。然而,最终的结果是,你同时告诉别人,因为你在重复她已经重复但没有返回的内容(她在你告诉你的班级的过程中打断了你她是否喜欢你)I'm going to give a completely non-technical answer on this one.
Let's say that there is a girl named Sally Function. You want to know if she likes you or not. So since you're in grade school you decide to pass Sally a note (call the function with parameters) asking her if she likes you or not. Now what you plan on doing is asking her this and then telling everyone what she tells you. Instead, you ask her and then she tells everyone. This is equivalent to returning (you getting the information and doing something with it) vs her echoing (telling everyone without you having any control).
In your case what is happening is that when Sally
echo
s she is taking the control from you and saying "I'm going to tell people this right now" instead of you being able to take her response and do what you wanted to do with it. The end result is, however, that you were telling people at the same time since you were echoing what she had already echoed but didn't return (she cut you off in the middle of you telling your class if she liked you or not)请考虑以下情况:
您可能期望输出为:
实际输出为:
原因是只要调用该函数,“Goodbye”就会被回显(写入)为输出。另一方面,“Hello”返回到
$hello
变量。$goodbye
实际上是空的,因为 goodbye 函数不返回任何内容。Consider the following:
You might expect the output to be:
The actual output is:
The reason is that "Goodbye" is echo'ed (written) as output, as soon as the function is called. "Hello", on the other hand, is returned to the
$hello
variable. the$goodbye
is actually empty, since the goodbye function does not return anything.我看到您仍然发表评论,这表明您很困惑,因为您不理解代码的流程。也许这会对您有所帮助(特别是 Mathias R. Jessen 的回答)。
因此,再次使用这两个函数:
现在,如果您执行以下操作:
您的屏幕上将留下“GoodbyeHello”。
原因如下。代码将像这样运行:
I see you are posting comments still which suggest you are confused because you don't understand the flow of the code. Perhaps this will help you (particularly with Mathias R. Jessen's answer).
So take these two functions again:
Now if you do this:
You will be left with 'GoodbyeHello' on your screen.
Here's why. The code will run like this:
因此,基本上,每当您想要向浏览器输出某些内容时,您都需要使用 echo,而当您想要结束脚本或函数并将数据传递到脚本的另一部分时,则需要使用 return。
So, basically you’ll want to use echo whenever you want to output something to the browser, and use return when you want to end the script or function and pass on data to another part of your script.
函数响应之间的区别在于“echo”向浏览器(DOM)发送一些内容,而“return”则向调用者返回一些内容。
The difference between the response of a function is that " echo" send something to the browser (DOM ) , while " return" returns something to the caller.
通过 return 函数本身可以被视为变量。
所以
会输出:
while
会输出:
因为add2没有
结果
。它所做的只是回显一些东西。永远不会真正将值返回给调用它的代码。顺便说一句,你并不傻。你只是一个初学者。我们都是初学者,一开始就需要克服一定的门槛。一开始您可能会遇到很多“愚蠢”的问题,但只要继续尝试,最重要的是实验,您就会学到东西。
with
return
the function itself can be treated somewhat like a variable.So
will output:
while
will output:
As there is no
result
of add2. What it does is only echo'ing out stuff. Never actually returning a value back to the code that called it.Btw, you are not dumb. You are just a beginner. We are all beginners in the beginning, and there is a certain threshold you'll need to get over in the beginning. You will probably have a lot of "dumb" questions in the beginning, but just keep on trying and above all experiment, and you will learn.
我在测试后发现有几个区别:
1)return只是返回函数的值,以便稍后将其存储在变量中后使用,但echo只是在调用函数时打印该值,并且不返回任何内容。
的简短示例
这是此函数 myfunc() {
echo "我是天生的程序员";
}
}
there is couple of difference i found after testing it
1) return just return the value of a function to get it used later after storing it in a variable but echo simply print the value as you call the function and returns nothing.
here is the short example for this
function myfunc() {
echo "i am a born programmer";
}
}
对您的示例稍作修改:
您能看到差异吗?
Using a slight modification of your example:
Can you see the difference?
echo
将文本等渲染到文档中,return
将数据从函数/方法等返回到调用它的任何地方。如果您回显返回,它将呈现它(假设它是文本/数字等 - 而不是对象等)。echo
renders the text etc into the document,return
returns data from a function/method etc to whatever called it. If you echo a return, it'll render it (Assuming it's text/number etc - not an object etc).在这两个函数后面都有一行,用于切换输出:
echo
打印该值,以便您可以读取它。return
返回要保存在例如变量中的值。Behind both functions you have a line, which toggles your output:
echo
prints the value so you can read it.return
returns the value to save in for example variables.基本上,要将 PHP 输出为 HTML,我们应该使用 echo。换句话说,我们需要回应它。
下面的两个示例将给出一个清晰的理解:
要在 html 中显示每个示例的 $result :
对于示例 1,我们应该使用
对于示例 2,我们应该使用 < code>
在示例 2 中,我们不需要 echo 它,因为我们在函数内部已经 echo 它。
Basically, to output PHP into HTML we should use echo. In other word, we need to echo it.
These two example below will give a clear understanding :
to show $result in html for each sample :
for sample 1 we should use
<?php echo $result ?>
for sample 2 we should use
<?php $result ?>
On sample 2 we do not need to echo it, because we have echo it inside the function.
在我看来,
echo
和return
之间最重要的区别是:每个结果的数据类型。
当我们编写如下所示的函数时:
是的,它们都会为我们提供 151 作为输出值。
但是,在
return
情况下,我们会将firstFunction($value)
打印为int
数据类型。< br>另一方面,在
echo
情况下,我们会将secondFunction($value)
打印为NULL
数据类型。您可以尝试使用
var_dump()
函数打印每个值来理解我的意思。当我们处理从数据库返回的某些值时,这种差异将使我们受益,特别是在数学运算中,例如(post观看次数)或类似的东西。
这对于这里所写的内容来说是有意义的。
希望我已经用简单的方式解释了它。
The most important difference between
echo
andreturn
in my viewpoint is:the data type of result for each one.
when we write some functions like below:
and yes, both of them will give us 151 as an output value.
But, in the
return
case, we will printfirstFunction($value)
as anint
data type.Otherhand, in the
echo
case, we will printsecondFunction($value)
as aNULL
data type.You can try printing each one with
var_dump()
function to understand what I meant.That difference will benefit us when we treat some values that returns from databases, especially in the math operations like (post views count) or something like that.
That'll make sense over what has been written here.
hope I have explained it the easy way.
我在 Buddypress 中进行更改时学到的一件事是,它主要在嵌套核心函数上使用 return,然后使用 sprintf 将动态变量绑定到 HTML 中,并将该 html 块返回到调用它的主函数只有这样它才会在主函数中回显一次。通过这样做,代码变得模块化并且更易于调试。
One thing that I learned while doing changes in Buddypress is that it uses the return mainly on nested core functions and then with the use of sprintf it binds dynamic variables into the HTML and return that chunck of html back to the main function where it was called and only then it echo out once at the main function. By doing so the code becomes modular and easier to debug.