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.
是的,我用谷歌搜索了这个问题,甚至提到了我的教科书(唐·戈斯林的PHP),但我似乎无法理解这个解释。
From my understanding:
根据我的理解:
echo = shows the final result of a function
echo =显示函数的最终结果
return = returns the value from a function
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?
两者都显示结果!我不明白的是什么?
13 个解决方案
#1
94
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).
假设有一个叫Sally Function的女孩。你想知道她是否喜欢你。所以,既然你在小学,你决定给莎莉一个音符(用参数调用函数),询问她是否喜欢你。现在你打算做的就是问她这个,然后告诉大家她告诉你什么。相反,你问她,然后她告诉所有人。这相当于返回(你获取信息并用它做某事)与她的回声(告诉所有人你没有任何控制)。
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)
在你的情况下,正在发生的事情是,当Sally回声时,她正在接受你的控制并说“我现在要告诉别人这个”,而不是你能够接受她的反应并做你想做的事情。然而,最终的结果是,你是在同时告诉别人,因为你回应了她已经回应但却没有回复的东西(在你告诉你的班级她是否喜欢你的时候,她会把你切断)
#2
39
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.
原因是,只要调用该函数,“Goodbye”就会作为输出进行回显(写入)。另一方面,“Hello”返回$ hello变量。 $ goodbye实际上是空的,因为再见函数不会返回任何内容。
#3
20
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.
#4
6
with return
the function itself can be treated somewhat like a variable.
返回时,函数本身可以像变量一样对待。
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.
因为没有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.
顺便问一下,你不是傻瓜。你只是一个初学者。我们在开始时都是初学者,并且在开始时你需要克服一定的门槛。你可能会在开始时遇到很多“愚蠢”的问题,但只要继续尝试,最重要的是实验,你就会学到。
#5
5
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,并将数据传递给脚本的另一部分。
#6
4
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)返回只返回一个函数的值,以便在将它存储在变量中之后使用它,但echo只需在调用函数时打印该值并且不返回任何值。
here is the short example for this
这是一个简短的例子
function myfunc() { echo "i am a born programmer"; }
function myfunc(){echo“我是一个天生的程序员”; }
$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";
}
}
#7
4
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 .
#8
3
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>";
?>
你能看到区别么?
#9
3
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将文本等呈现到文档中,返回从函数/方法等返回数据到任何调用它的数据。如果你回显一个回归,它将呈现它(假设它是文本/数字等 - 而不是对象等)。
#10
2
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;
#11
1
Basically, to output PHP into HTML we should use echo. In other word, we need to echo it.
基本上,要将PHP输出为HTML,我们应该使用echo。换句话说,我们需要回应它。
These two example below will give a clear understanding :
下面的这两个例子将给出一个清晰的理解:
function myfunction() {
// script content here, and sample out maybe like this :
return $result; ---> sample 1
echo $result; ---> sample 2
}
to show $result in html for each sample :
为每个样本显示$ html的结果:
for sample 1 we should use <?php echo $result ?>
对于样本1,我们应该使用<?php echo $ result?>
for sample 2 we should use <?php $result ?>
对于样本2,我们应该使用<?php $ result?>
On sample 2 we do not need to echo it, because we have echo it inside the function.
在示例2中,我们不需要回显它,因为我们在函数内部回显它。
#12
0
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.
我在Buddypress中进行更改时学到的一件事是它主要在嵌套核心函数上使用返回,然后使用sprintf将动态变量绑定到HTML中并将html的chunck返回到调用它的main函数中然后它只在主函数回显一次。通过这样做,代码变得模块化并且更容易调试。
#13
0
The most important difference between echo
and return
in my viewpoint is:
the data type of result for each one.
when we write some functions like below:
在我看来,echo和return之间最重要的区别是:每个结果的数据类型。当我们编写如下函数时:
<?php
$value = 150;
function firstFunction($value) {
return $value + 1;
}
echo firstFunction($value) . '<br />';
function secondFunction($value) {
echo $value + 1;
}
secondFunction($value);
and yes, both of them will give us 151 as an output value.
But, in the return
case, we will print firstFunction($value)
as an int
data type.
Otherhand, in the echo
case, we will print secondFunction($value)
as a NULL
data type.
You can try printing each one with var_dump()
function to understand what I meant.
是的,他们两个都会给我们151作为输出值。但是,在返回的情况下,我们将打印firstFunction($ value)作为int数据类型。另外,在echo情况下,我们将打印secondFunction($ value)作为NULL数据类型。您可以尝试使用var_dump()函数打印每个函数以了解我的意思。
<?php
var_dump(firstFunction($value)); ?>
<br />
<?php
var_dump(secondFunction($value));
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.
当我们处理从数据库返回的某些值时,这种差异将使我们受益,特别是在数学运算中,如(post views count)或类似的东西。这对于这里写的内容有意义。希望我能以简单的方式解释它。
#1
94
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).
假设有一个叫Sally Function的女孩。你想知道她是否喜欢你。所以,既然你在小学,你决定给莎莉一个音符(用参数调用函数),询问她是否喜欢你。现在你打算做的就是问她这个,然后告诉大家她告诉你什么。相反,你问她,然后她告诉所有人。这相当于返回(你获取信息并用它做某事)与她的回声(告诉所有人你没有任何控制)。
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)
在你的情况下,正在发生的事情是,当Sally回声时,她正在接受你的控制并说“我现在要告诉别人这个”,而不是你能够接受她的反应并做你想做的事情。然而,最终的结果是,你是在同时告诉别人,因为你回应了她已经回应但却没有回复的东西(在你告诉你的班级她是否喜欢你的时候,她会把你切断)
#2
39
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.
原因是,只要调用该函数,“Goodbye”就会作为输出进行回显(写入)。另一方面,“Hello”返回$ hello变量。 $ goodbye实际上是空的,因为再见函数不会返回任何内容。
#3
20
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.
#4
6
with return
the function itself can be treated somewhat like a variable.
返回时,函数本身可以像变量一样对待。
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.
因为没有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.
顺便问一下,你不是傻瓜。你只是一个初学者。我们在开始时都是初学者,并且在开始时你需要克服一定的门槛。你可能会在开始时遇到很多“愚蠢”的问题,但只要继续尝试,最重要的是实验,你就会学到。
#5
5
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,并将数据传递给脚本的另一部分。
#6
4
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)返回只返回一个函数的值,以便在将它存储在变量中之后使用它,但echo只需在调用函数时打印该值并且不返回任何值。
here is the short example for this
这是一个简短的例子
function myfunc() { echo "i am a born programmer"; }
function myfunc(){echo“我是一个天生的程序员”; }
$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";
}
}
#7
4
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 .
#8
3
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>";
?>
你能看到区别么?
#9
3
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将文本等呈现到文档中,返回从函数/方法等返回数据到任何调用它的数据。如果你回显一个回归,它将呈现它(假设它是文本/数字等 - 而不是对象等)。
#10
2
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;
#11
1
Basically, to output PHP into HTML we should use echo. In other word, we need to echo it.
基本上,要将PHP输出为HTML,我们应该使用echo。换句话说,我们需要回应它。
These two example below will give a clear understanding :
下面的这两个例子将给出一个清晰的理解:
function myfunction() {
// script content here, and sample out maybe like this :
return $result; ---> sample 1
echo $result; ---> sample 2
}
to show $result in html for each sample :
为每个样本显示$ html的结果:
for sample 1 we should use <?php echo $result ?>
对于样本1,我们应该使用<?php echo $ result?>
for sample 2 we should use <?php $result ?>
对于样本2,我们应该使用<?php $ result?>
On sample 2 we do not need to echo it, because we have echo it inside the function.
在示例2中,我们不需要回显它,因为我们在函数内部回显它。
#12
0
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.
我在Buddypress中进行更改时学到的一件事是它主要在嵌套核心函数上使用返回,然后使用sprintf将动态变量绑定到HTML中并将html的chunck返回到调用它的main函数中然后它只在主函数回显一次。通过这样做,代码变得模块化并且更容易调试。
#13
0
The most important difference between echo
and return
in my viewpoint is:
the data type of result for each one.
when we write some functions like below:
在我看来,echo和return之间最重要的区别是:每个结果的数据类型。当我们编写如下函数时:
<?php
$value = 150;
function firstFunction($value) {
return $value + 1;
}
echo firstFunction($value) . '<br />';
function secondFunction($value) {
echo $value + 1;
}
secondFunction($value);
and yes, both of them will give us 151 as an output value.
But, in the return
case, we will print firstFunction($value)
as an int
data type.
Otherhand, in the echo
case, we will print secondFunction($value)
as a NULL
data type.
You can try printing each one with var_dump()
function to understand what I meant.
是的,他们两个都会给我们151作为输出值。但是,在返回的情况下,我们将打印firstFunction($ value)作为int数据类型。另外,在echo情况下,我们将打印secondFunction($ value)作为NULL数据类型。您可以尝试使用var_dump()函数打印每个函数以了解我的意思。
<?php
var_dump(firstFunction($value)); ?>
<br />
<?php
var_dump(secondFunction($value));
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.
当我们处理从数据库返回的某些值时,这种差异将使我们受益,特别是在数学运算中,如(post views count)或类似的东西。这对于这里写的内容有意义。希望我能以简单的方式解释它。