Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Functions Function Returns and More Returning Values

Miguel Nunez
Miguel Nunez
3,266 Points

What's the difference between an echo and a return.?

I notice some examples like for example math situations will tend to give me a situation that equals the answer so for example 1+1= 2 I notice echo and a return gives the same answer and response when I see an execution of it if they as so different what are they doing given the same results and same effects why would I bother with a return if I can just do the same thing with just use an echo all the time.

Jesus Mendoza
Jesus Mendoza
23,289 Points
  • "echo" prints the result on screen and you can't do anything with that value, its just printed on screen.
  • "return" returns it from the function and you can do whatever you need with that value. I don't know much about PHP but in JavaScript you can store that value in a variable and use it later, pass it to another function, etc.
Aayatullah Miyan
Aayatullah Miyan
4,687 Points

At first part the value is passed from addup(10,15) to the addup($a,$b). Then the sum is done in $c and it is echoed. In secord part, the value is passed from multi(10,15) to the multi($num1,$num2) then the product is returned using return to multi() in the line containing echo "<br> The value of the product is". multi(10,15) making it echo the product.

<?php
function addup($a,$b){
$c=$a+$b;
echo $c." This is the sum";
}
addup(10,15);
 ?>
//
 <?php
function multi($num1,$num2){
  return $num1*$num2;
}
echo "<br>The value of the product is " .multi(10,15);

  ?>```