What is the difference between echo and return in php?

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 "<h2 style='font-family:Helvetica; color:red'>Using <em>echo</em></h2>"; function add1($x, $y){ $total = $x + $y; echo $total; } echo "<p>2 + 2 = ", add1(2, 2), "</p>"; echo "<h2 style='font-family:Helvetica; color:red'>Using <em>return</em></h2>"; 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?

Déjà vu

7982 gold badges9 silver badges29 bronze badges

asked Feb 22, 2012 at 1:03

2

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)

answered Feb 22, 2012 at 2:30

jprofittjprofitt

10.8k4 gold badges34 silver badges45 bronze badges

4

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.

answered Feb 22, 2012 at 1:13

Mathias R. JessenMathias R. Jessen

141k11 gold badges134 silver badges185 bronze badges

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.

answered Mar 28, 2014 at 12:18

BadHorsieBadHorsie

13.8k29 gold badges112 silver badges183 bronze badges

1

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.

answered Nov 22, 2015 at 18:06

Mr GreenMr Green

891 silver badge1 bronze badge

1

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.

answered Feb 22, 2012 at 1:09

XyzXyz

5,7025 gold badges40 silver badges57 bronze badges

6

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 .

answered Mar 10, 2015 at 16:42

AlexAlex

711 silver badge1 bronze badge

1

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

}

answered Oct 1, 2014 at 6:09

Using a slight modification of your example:

<?php echo "<h2 style='font-family:Helvetica; color:red'>Using <em>echo</em></h2>"; function add1($x, $y){ $total = $x + $y; echo $total; } $result = add1(2, 2); echo "<p>2 + 2 = ", $result, "</p>"; echo "<h2 style='font-family:Helvetica; color:red'>Using <em>return</em></h2>"; function add2($x, $y){ $total = $x + $y; return $total; } $result = add2(2, 2); echo "<p>2 + 2 = ", $result, "</p>"; ?>

Can you see the difference?

answered Feb 22, 2012 at 1:07

Daniel PrydenDaniel Pryden

57.8k15 gold badges96 silver badges134 bronze badges

0

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

answered Feb 22, 2012 at 1:05

BenOfTheNorthBenOfTheNorth

2,8841 gold badge19 silver badges46 bronze badges

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;

answered Feb 22, 2012 at 1:07

SmamattiSmamatti

3,8913 gold badges31 silver badges42 bronze badges

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.

answered Jan 8, 2015 at 20:42

user3706926user3706926

1811 silver badge11 bronze badges

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.

answered Jul 8, 2016 at 3:33

zawzaw

6641 gold badge10 silver badges30 bronze badges

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.

answered Sep 16, 2016 at 4:32

What is the difference between echo and print in PHP?

echo and print are more or less the same. They are both used to output data to the screen. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions.

What is echo return?

Unlike some other language constructs, echo does not have any return value, so it cannot be used in the context of an expression. echo also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign.

What is a return in PHP?

The return keyword ends a function and, optionally, uses the result of an expression as the return value of the function. If return is used outside of a function, it stops PHP code in the file from running.

What is the difference between return and return value?

Return by value is like the artist handing you a printed copy of the boat they drew. Return by reference is like the artist telling you the address of the house they left your picture in.

Chủ đề