是的,我已经用谷歌搜索过这个问题,甚至参考了我的教科书(Don Gosselin的PHP(,但我似乎真的无法理解解释。
据我了解:
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>";
?>
两者都显示结果!我不明白什么?
对此给出一个完全非技术性的答案。
假设有一个女孩叫莎莉功能。你想知道她是否喜欢你。因此,由于你在上小学,你决定给莎莉一张纸条(用参数调用函数(,问她是否喜欢你。现在你打算做的是问她这个,然后告诉每个人她告诉你什么。相反,你问她,然后她告诉所有人。这相当于返回(你得到信息并用它做点什么(与她的回声(告诉你没有任何控制的人(。
在你的情况下,正在发生的事情是,当莎莉echo
她正在从你手中夺取控制权并说"我现在要告诉人们这件事",而不是你能够接受她的回应并做你想做的事情。然而,最终的结果是,你同时告诉人们,因为你在回响她已经回声但没有回来的东西(她在你告诉你的班级她是否喜欢你的时候打断了你(
请考虑以下事项:
<?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
?>
您可能期望输出为:
HelloGoodbye
实际输出为:
GoodbyeHello
原因是一旦调用函数,"再见"就会作为输出回显(写入(。另一方面,"Hello"返回到$hello
变量。$goodbye
实际上是空的,因为 goodbye 函数不返回任何内容。
我看到您仍然在发布评论,这表明您感到困惑,因为您不了解代码的流程。也许这会对你有所帮助(特别是Mathias R. Jessen的回答(。
因此,再次使用这两个函数:
function sayHelloLater() {
return 'Hello';
}
function sayGoodbyeNow() {
echo 'Goodbye';
}
现在,如果您这样做:
$hello = sayHelloLater();
$goodbye = sayGoodbyeNow();
echo $hello;
echo $goodbye;
您将在屏幕上留下"再见你好"。
原因如下。代码将像这样运行:
$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.
因此,基本上,每当您想要向浏览器输出某些内容时,您都会希望使用 echo,并在想要结束脚本或函数并将数据传递给脚本的另一部分时使用 return。
函数响应的区别在于 "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 .
return
函数本身可以被视为变量。
所以
return add1(2, 3) + add1(10, 10);
将输出:
25
而
echo add2(2, 3) + add2(10, 10);
将输出:
5
20
0
因为没有 add2 的result
。它所做的只是回声。从不实际将值返回给调用它的代码。
顺便说一句,你不傻。你只是一个初学者。我们一开始都是初学者,一开始你需要克服一定的门槛。一开始你可能会有很多"愚蠢"的问题,但只要继续尝试,最重要的是实验,你就会学到。
后我发现了一些不同之处
1( return 只是返回函数的值,以便在将其存储在变量中后稍后使用它,但 echo 只是在调用函数时打印值并且不返回任何内容。
这是这个的简短示例
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";
}
对示例稍作修改:
<?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>";
?>
你能看出区别吗?
echo
将文本等呈现到文档中,return
将数据从函数/方法等返回到调用它的任何内容。如果你回显一个返回,它会渲染它(假设它是文本/数字等 - 而不是一个对象等(。
在这两个函数的后面,你都有一行,用于切换你的输出:
echo "<p>2 + 2 = ", add1(2, 2), "</p>";
echo "<p>2 + 2 = ", add2(2, 2), "</p>";
echo
打印值,以便您可以读取它。 return
返回要保存在示例变量中的值。
$result = add2(2, 2);
// do more with result ... ?
// output the result
echo $result;
基本上,要将PHP输出到HTML中,我们应该使用echo。换句话说,我们需要回应它。
下面的这两个例子将给出一个清晰的理解:
function myfunction() {
// script content here, and sample out maybe like this :
return $result; ---> sample 1
echo $result; ---> sample 2
}
要在 HTML 中显示每个示例的$result:
对于样本 1,我们应该使用 <?php echo $result ?>
对于示例 2,我们应该使用 <?php $result ?>
示例 2 中,我们不需要回显它,因为我们在函数内部回显了它。
,echo
和return
之间最重要的区别是:
每个结果的数据类型.
当我们编写一些函数时,如下所示:
<?php
$value = 150;
function firstFunction($value) {
return $value + 1;
}
echo firstFunction($value) . '<br />';
function secondFunction($value) {
echo $value + 1;
}
secondFunction($value);
是的,它们都会给我们 151 作为输出值。
但是,在return
的情况下,我们将firstFunction($value)
打印为int
数据类型.
另一方面,在echo
的情况下,我们将secondFunction($value)
打印为NULL
数据类型.
您可以尝试使用var_dump()
函数打印每个以了解我的意思.
<?php
var_dump(firstFunction($value)); ?>
<br />
<?php
var_dump(secondFunction($value));
当我们处理从数据库返回的一些值时,这种差异将使我们受益,尤其是在数学运算中,例如(帖子浏览量(或类似的东西.
这比这里写的内容有意义.
希望我已经用简单的方法解释了它。
我在Buddypress中进行更改时学到的一件事是,它主要在嵌套的核心函数上使用返回,然后使用sprintf将动态变量绑定到HTML中,并将html的chuck返回到调用它的主函数,然后它才在主函数处回显一次。通过这样做,代码变得模块化并且更易于调试。