What is the difference between PHP echo and PHP return in plain English?
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)
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.
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.
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.