PHP echo 和 PHP return 之间的区别是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9387765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between PHP echo and PHP return in plain English?
提问by Joe Morales
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.
是的,我在谷歌上搜索了这个问题,甚至参考了我的教科书(Don Gosselin 的 PHP),但我似乎真的无法理解解释。
From my understanding:
根据我的理解:
echo = shows the final result of a function
return = returns the value from a function
echo = 显示函数的最终结果
return = 从函数返回值
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
.
我同时应用了这两个函数,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>";
?>
Both display the result! What am I not understanding?
两者都显示结果!我不明白什么?
回答by jprofitt
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 shetells 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).
假设有一个名叫 Sally Function 的女孩。你想知道她喜不喜欢你。因此,因为您在上小学,所以您决定给 Sally 一个便条(调用带参数的函数),询问她是否喜欢您。现在你打算做的是问她这个,然后告诉每个人她告诉你的话。相反,你问她,然后她告诉每个人。这相当于返回(你获取信息并用它做某事)与她的回应(在你没有任何控制的情况下告诉所有人)。
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 youtelling your class if she liked you or not)
在你的情况下,发生的事情是,当 Sallyecho
控制你并说“我现在要告诉人们这个”而不是你能够接受她的回应并做你想做的事情. 然而,最终的结果是,你同时告诉别人,因为你在回应她已经回应但没有返回的内容(她在你告诉你的班级她是否喜欢你的时候打断了你)
回答by Mathias R. Jessen
Consider the following:
考虑以下:
<?php
function sayHelloLater(){
return "Hello";
}
function sayGoodbyeNow(){
echo "Goodbye";
}
$hello = sayHelloLater(); // "Hello" returned and stored in $hello
$goodbye = sayGoodbyeNow(); // "Goodbye" is echo'ed and nothing is returned
echo $hello; // "Hello" is echo'ed
echo $goodbye; // nothing is echo'ed
?>
You might expect the output to be:
您可能希望输出为:
HelloGoodbye
The actual output is:
实际输出为:
GoodbyeHello
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.
原因是“再见”作为输出被回显(写入),只要函数被调用。另一方面,“Hello”被返回给$hello
变量。在$goodbye
实际上是空的,因为再见功能不返回任何东西。
回答by BadHorsie
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).
我看到您仍在发布评论,这表明您很困惑,因为您不了解代码的流程。也许这会对您有所帮助(尤其是Mathias R. Jessen 的回答)。
So take these two functions again:
所以再次使用这两个函数:
function sayHelloLater() {
return 'Hello';
}
function sayGoodbyeNow() {
echo 'Goodbye';
}
Now if you do this:
现在如果你这样做:
$hello = sayHelloLater();
$goodbye = sayGoodbyeNow();
echo $hello;
echo $goodbye;
You will be left with 'GoodbyeHello' on your screen.
您的屏幕上将显示“GoodbyeHello”。
Here's why. The code will run like this:
这是为什么。代码将像这样运行:
$hello = sayHelloLater(); ---->-------->-------->------->------>--
|
| ^ |
| | Call the function
v | |
| ^ |
| | v
|
v "return" simply sends back function sayHelloLater() {
| 'Hello' to wherever the <----<---- return 'Hello';
| function was called. }
v Nothing was printed out
| (echoed) to the screen yet.
|
v
$hello variable now has whatever value
the sayHelloLater() function returned,
so $hello = 'Hello', and is stored for
whenever you want to use it.
|
|
v
|
|
v
$goodbye = sayGoodbyeNow(); ---->-------->-------->------->------
|
| ^ |
| | Call the function
v | |
| ^ |
| | v
| |
v | function sayGoodbyeNow() {
| echo 'Goodbye';
| The function didn't return }
| anything, but it already
v printed out 'Goodbye' |
| v
| ^
| | "echo" actually prints out
v <-----------<-----------<--------- the word 'Goodbye' to
| the page immediately at
| this point.
|
v
Because the function sayGoodbyeNow() didn't
return anything, the $goodbye variable has
a value of nothing (null) as well.
|
|
|
v
|
|
|
v
echo $hello; -------->-------> Prints 'Hello' to the screen at
this point. So now your screen says
| 'GoodbyeHello' because 'Goodbye' was
| already echoed earlier when you called
| the sayGoodbyeNow() function.
v
echo $goodbye; ------>-------> This variable is null, remember? So it
echoes nothing.
|
|
|
v
And now your code is finished and you're left with
'GoodbyeHello' on your screen, even though you echoed
$hello first, then $goodbye.
回答by Aron Cederholm
with return
the function itself can be treated somewhat like a variable.
与return
函数本身可以有点像一个变量。
So
所以
return add1(2, 3) + add1(10, 10);
will output:
将输出:
25
while
尽管
echo add2(2, 3) + add2(10, 10);
will output:
将输出:
5
20
0
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.
因为没有result
add2。它所做的只是呼应出的东西。永远不要真正将值返回给调用它的代码。
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.
顺便说一句,你不傻。你只是一个初学者。一开始我们都是初学者,一开始你需要克服一定的门槛。一开始你可能会有很多“愚蠢”的问题,但只要继续尝试,最重要的是实验,你就会学到东西。
回答by Mr Green
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,并在您想结束脚本或函数并将数据传递给脚本的另一部分时使用 return。
回答by Alex
The difference between the response of a function is that " echo" send something to the browser (DOM ) , while " return" returns something to the caller.
函数响应之间的区别在于“echo”向浏览器(DOM)发送一些东西,而“return”向调用者返回一些东西。
function myFunction(
return 5;
}
$myVar= myFunction(); //myVar equals 5
echo $myVar; // will show a "5 " on the screen
function myFunction() {
echo 5;
}
$myVar= myFunction(); // myVar equals 0, but the screen gets a "5"
echo $myVar; // a zero on the screen next to "5" printed by function appears .
回答by sandeep_kosta
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.
1) return 只返回函数的值,以便在将其存储在变量中后稍后使用它,但 echo 只是在调用函数时打印该值,并且不返回任何内容。
here is the short example for this
这是一个简短的例子
function myfunc() {
echo "i am a born programmer";
}
function myfunc() {
echo "i am a born programmer";
}
$value = myfunc(); \ it is going to print the 'i am a born programmer' as function would be called
if(empty($value)===true) {
echo "variable is empty because function returns nothing";
}
}
回答by BenOfTheNorth
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
将数据从函数/方法等返回到调用它的任何地方。如果您回显返回,它将呈现它(假设它是文本/数字等 - 而不是对象等)。
回答by Daniel Pryden
Using a slight modification of your example:
对您的示例稍作修改:
<?php
echo "<h1 style='font-family:Helvetica; color:red'>Using <em>echo</em></h1>";
function add1($x, $y){
$total = $x + $y;
echo $total;
}
$result = add1(2, 2);
echo "<p>2 + 2 = ", $result, "</p>";
echo "<h1 style='font-family:Helvetica; color:red'>Using <em>return</em></h1>";
function add2($x, $y){
$total = $x + $y;
return $total;
}
$result = add2(2, 2);
echo "<p>2 + 2 = ", $result, "</p>";
?>
回答by Smamatti
Behind both functions you have a line, which toggles your output:
在这两个函数后面有一条线,用于切换输出:
echo "<p>2 + 2 = ", add1(2, 2), "</p>";
echo "<p>2 + 2 = ", add2(2, 2), "</p>";
echo
prints the value so you can read it.
return
returns the value to save in for example variables.
echo
打印值,以便您可以读取它。
return
返回要保存在例如变量中的值。
$result = add2(2, 2);
// do more with result ... ?
// output the result
echo $result;