What is the difference between PHP echo and PHP return in plain English?

68,679

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

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

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

Solution 4

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.

Solution 5

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.

Share:
68,679
Joe Morales
Author by

Joe Morales

Look me up at @astroXP

Updated on September 16, 2020

Comments

  • Joe Morales
    Joe Morales over 3 years

    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?