using "return" or "echo" in function

Good day php help,

I have a question regarding “return” and “echo” in a function,
below is a code

[php]

<?php function addition($a,$b) { $c=$a+$b; return $c; } echo "9 + 4 = ".addition(9,4); ?> [/php]

Output: 9 + 4 = 13

[php]

<?php function addition($a,$b) { $c=$a+$b; echo $c; } echo "9 + 4 = ".addition(9,4); ?> [/php]

Output: 139 + 4 =

if I use “echo” why won’t it works the same with return?

<?php function addition($a,$b) { $c=$a+$b; echo $c; } ?>

<!doctype html>

Document

9 + 4 = <?= addition(9,4); ?>

Echo strings, return values is the easiest way to explain the difference.

Thanks Strider64 and scottlpool2003,

I kinnda understand :slight_smile:

Just for fun I did try what you where doing and I have to say that was some weird result, I can see where it could be confusing. The way I look at it is this, if you don’t need a value back and just want to display it to the screen the use echo, otherwise use return to assign it to a variable. The to be honest in my opinion it’s easier to use return and assign it to a variable.

Unless you have a specific reason not to do it this way, you should always return a value in a function. This is even more important when dealing with class methods. Now, you can output instead if you have reason, I will build forms inside of functions and call them when needed, because using that in a return makes it more complicated.

I see, but I really don’t understand why the value of a function comes first before the "9 + 4 = " in this example?

[php]

<?php function addition($a,$b) { $c=$a+$b; echo $c; } echo "9 + 4 = ".addition(9,4); ?> [/php]

Result: 139 + 4 =

I was expecting for “9 + 4 = 13” is it because the function addition is above the

echo "9 + 4 = ".addition(9,4); ?

so I tried writing the function after the

echo "9 + 4 = ".addition(9,4);

[php]

<?php echo "9 + 4 = ".addition(9,4); function addition($a,$b) { $c=$a+$b; echo $c; } ?> [/php]

Result: 139 + 4 =

still the same result… why is this? :frowning:

If you have ever used C or Java, you know that to make a function you have to say, for example
[php]void win() {
//You won!
}[/php]
The void on the beginning means that the function doesn’t return anything. If you wanted it to, you would say String win() {} for example. Now to PHP:
[php]
//Echo method
function win() {
echo ‘You won! Congratulations!’;
}
win();

//Return method
function win() {
return ‘You won! Congratulations!’;
}

echo win();
[/php]
As you can see, return uses slightly more code. But what is the point of it? Why would you use more code? As with many things, it is for forward compatiblity (I think thats what its called :)). Basically, you want to be able to use this in the future. What if, in the future, you want to lowercase the entire thing. Why would you do that, I don’t know, but there are real world implications. Here is how you would do it:
[php]
//Echo method
function win($lower = false) {
if($lower) {
echo ‘You won! Congratulations!’;
} else {
echo strtolower(‘You won! Congratulations!’;
}
win();
win(true);

//Return method
function win() {
return ‘You won! Congratulations!’;
}

echo win();
echo strtolower(win());
[/php]
Hope this helps!

Now look at this example doing it, properly in this case.

[php]

<?php
function addition($a,$b) {
   return $a + $b;
   
}

echo "9 + 4 = " . addition(9,4);
?>

[/php]

Thanks Mr.Russ,

but I don’t get it… it doesn’t answer any of my question…

I have a code here

[php]

<?php
function addition($a,$b) {
   $c=$a+$b;
   echo $c;
}

echo "9 + 4 = ".addition(9,4);
?>

[/php]

the output is: “139 + 4 =”

My question is:

139 + 4 =” why is the output of the function addition($a,$b) which is “13” in this example is positioned before the “9+4”?

I was expecting the output of the "echo "9 + 4 = “.addition(9,4);” to be “9 + 4 = 13” because I called the function addition() after the text “9 + 4”?

Thanks Mr.astonecipher, the code is almost similar to my very first code.

[code] function addition($a,$b) {
return $a + $b;

  }[/code]

is better than this?

[php] function addition($a,$b) {
$c=$a+$b;
echo $c;
}[/php]

Hey guys,

Just want to put my 2 cents in:

I believe in PHP, functions are performed before any type of output. If you have an echo inside of a function, the echo inside of that function will get processed before any type of echo outside of the function due to functions getting processed first. Kind of like (2+3) / 2: (2+3) will be performed before the / 2. You would use the RETURN keyword to throw the value back outside of the function. The value 13 will be sent back for the echo to echo it out later.

Take a look at this:

[php]if(isValid())[/php]

The function gets performed first then it’s taken in by the IF statement; this is the same as the ECHO. This is why you get 139 + 4. Hope this explains things.

Cheers!

Different lesson but comes into this.

When dealing with data, you want to return it not display it right off. Some other process may need to use what is being returned, in true OOP style.

What is happening is, you are echoing an echo. Try your code with one slight twist, instead of doing it this way,

[php] echo "9 + 4 = ".addition(9,4);[/php]

Try it this way.

[php]
echo "9 + 4 = ";
addition(9,4);
[/php]

Thank you Mr.astonecipher,

now I understand when you mentioned “you are echoing an echo.

you made my day and it works! :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service