What is the difference between PHP echo and PHP re

2019-01-01 15:14发布

问题:

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?

回答1:

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 echos 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)



回答2:

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.



回答3:

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).

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.

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:

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.

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:

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.



回答6:

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.

here is the short example for this

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\"; 

}



回答7:

The difference between the response of a function is that \" echo\" send something to the browser (DOM ) , while \" return\" returns something to the caller.

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:

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>\";

?>

Can you see the difference?



回答9:

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).



回答10:

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.

$result = add2(2, 2);
// do more with result ... ?
// output the result
echo $result;


回答11:

Basically, to output PHP into HTML we should use echo. In other word, we need to echo it.

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 :

for sample 1 we should use <?php echo $result ?>

for sample 2 we should use <?php $result ?>

On sample 2 we do not need to echo it, because we have echo it inside the function.



回答12:

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.



回答13:

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:

<?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.

<?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.