PHP echo和PHP return在简单英语中的区别是什么?
是的,我在谷歌上搜索了这个问题,甚至还参考了我的教科书(唐·戈塞林的PHP),但我似乎真的无法理解这个解释 据我了解: echo=显示函数的最终结果 return=从函数返回值 我在以下函数中应用了PHP echo和PHP return在简单英语中的区别是什么?,php,function,return,echo,Php,Function,Return,Echo,是的,我在谷歌上搜索了这个问题,甚至还参考了我的教科书(唐·戈塞林的PHP),但我似乎真的无法理解这个解释 据我了解: echo=显示函数的最终结果 return=从函数返回值 我在以下函数中应用了echo和return,我看不出使用return而不是echo的区别或“有效性” <?php echo "<h1 style='font-family:Helvetica; color:red'>Using <em>echo</em></h1>";
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
将文本等呈现到文档中,return
将函数/方法等的数据返回给调用它的任何对象。如果您回显一个返回,它将呈现它(假设它是文本/数字等,而不是对象等)。对您的示例稍加修改:
<?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 "<p>2 + 2 = ", add1(2, 2), "</p>";
echo "<p>2 + 2 = ", add2(2, 2), "</p>";
使用return
可以将函数本身视为变量
所以
将输出:
25
5
20
0
当
将输出:
25
5
20
0
因为没有add2的结果。它所做的只是呼应出一些东西。从不将值实际返回给调用它的代码
顺便说一句,你不是傻瓜。你只是个初学者。我们在一开始都是初学者,在一开始你需要克服一定的门槛。一开始你可能会有很多“愚蠢”的问题,但只要继续尝试,首先是实验,你就会学到东西。考虑以下几点:
<?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
?>
实际输出为:
GoodbyeHello
原因是,一旦调用函数,“再见”就会作为输出进行回显(写入)。另一方面,“Hello”返回到$Hello
变量。$bye
实际上是空的,因为bye函数不返回任何内容。我将给出一个完全非技术性的答案
让我们假设有一个女孩叫莎莉。你想知道她是否喜欢你。因此,既然你在上小学,你决定给Sally一张便条(用参数调用函数),问她是否喜欢你。现在你打算做的是问她这个,然后告诉所有人她告诉你的。相反,你问她,然后她告诉大家。这相当于返回(你得到了信息并用它做了些什么)与她的回应(在你没有任何控制的情况下告诉所有人)
在你的情况下,发生的是当Sallyecho
s从你手中接过控制权,说“我现在就要告诉别人这件事”,而不是你能够接受她的回应,做你想做的事情。然而,最终的结果是,你同时告诉别人,因为你在回荡她已经回荡但却没有回来的东西(她在你中间把你切掉了,告诉她你喜欢不喜欢你的班级)我看到您仍在发布评论,这表明您感到困惑,因为您不了解代码的流程。也许这会对你有所帮助(特别是在工作上)
因此,请再次使用这两个函数:
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.
$hello=sayHelloLater();-->-->--
¦
¦ ^ ¦
调用函数
伏特
¦ ^ ¦
阿尔法
¦
v“return”只是发回函数sayHelloLater(){
无论在哪里,只要是--------------------------->------
¦
¦ ^ ¦
调用函数
伏特
¦ ^ ¦
阿尔法
¦ ¦
函数sayGoodbyeNow(){
“再见”的回声;
函数没有返回}
什么都可以,但已经有了
v打印出“再见”字母
阿尔夫
¦ ^
阿尔法“回声”实际上是打印出来的
v测试后,我发现有两个差异
1) return只返回函数的值,以便在将其存储在变量中之后使用,但echo只需在调用函数时打印该值,而不返回任何内容
下面是一个简短的例子
函数myfunc(){
回声“我是一个天生的程序员”;
}
$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输出到HTML中,我们应该使用echo。换句话说,我们需要回应它
以下两个示例将给出一个清晰的理解:
function myfunction() {
// script content here, and sample out maybe like this :
return $result; ---> sample 1
echo $result; ---> sample 2
}
要在html中为每个示例显示$result,请执行以下操作:
对于示例1,我们应该使用
对于示例2,我们应该使用
在示例2中,我们不需要回显它,因为我们在函数内部有回显功能。函数响应之间的区别在于“回显”将某些内容发送到浏览器(DOM),而“返回”将某些内容返回给调用方
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 .
因此,基本上,只要您想将内容输出到浏览器,您就需要使用echo,而在您需要时使用return
function myfunction() {
// script content here, and sample out maybe like this :
return $result; ---> sample 1
echo $result; ---> sample 2
}
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 .
<?php
$value = 150;
function firstFunction($value) {
return $value + 1;
}
echo firstFunction($value) . '<br />';
function secondFunction($value) {
echo $value + 1;
}
secondFunction($value);
<?php
var_dump(firstFunction($value)); ?>
<br />
<?php
var_dump(secondFunction($value));