简单英文中的PHP echo和PHP return有什么区别? [英] What is the difference between PHP echo and PHP return in plain English?
问题描述
是的,我已经使用了这个问题,甚至提到了我的教科书(由Don Gosselin编写的PHP),但我认真地理解不了解释。
从我的理解是:
$ b
echo =显示函数的最终结果
return =返回函数的值
我应用了 echo
和 return
在以下函数中,我看不到使用 return
而不是<$ c $的区别或有效性 c> echo 。
<?php
echo< h1 style = 'font-family:Helvetica; color:red'>使用< em> echo< / em>< / h1>;
函数add1($ x,$ y){
$ total = $ x + $ y;
echo $ total;
}
echo< p> 2 + 2 =,add1(2,2),< / p>;
$ b $ echo'< h1 style ='font-family:Helvetica; color:red'>使用< em> return< / em>< / h1>;
函数add2($ x,$ y){
$ total = $ x + $ y;
return $ total;
}
echo< p> 2 + 2 =,add2(2,2),< / p>;
?>
两者都显示结果!
我不理解的是什么?
我会给出一个完全非技术性的答案。
假设有一个名为Sally Function的女孩。你想知道她是否喜欢你。所以既然你在小学,你决定通过Sally一个音符(用参数调用该函数)询问她是否喜欢你。现在你要做的就是问问她,然后告诉大家她告诉你的是什么。相反,你问她,然后她告诉每个人。这相当于返回(你得到的信息和做的事情)与她的回应(告诉每个人没有任何控制权)。
在你的情况下,发生的事情是当Sally echo
s时,她正在控制你并说我现在要告诉别人,而不是你能够采取她的回应,并做你想做的事情。然而,最终的结果是,你同时告诉别人,因为你回应了她已经回应但未回来的内容(她在你的中间阻止了你告诉你的班级如果她喜欢你或不)
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?
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)
这篇关于简单英文中的PHP echo和PHP return有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!