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 Introducing Functions Introducing Functions

Miguel Nunez
Miguel Nunez
3,266 Points

When people try to explain what a return means in php what is it meant when they say this.

returns the value from a function.

1 Answer

andren
andren
28,558 Points

They simply mean that return is used as a means for a function to give a value back to what called it, the actual value that is returned is whatever follows the return statement. So "return $something" would return whatever was stored within the "something" variable.

Let's say you wanted to make a function that added two numbers together and then gave you the answer, to do that you have to use return like this:

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

$result = sum(5, 10);
// $result now hold the value 15, as that is the result that the sum method returned.
?>

You can pretty much think of it as doing the reverse of supplying an argument to a function. Instead of passing a value to the function, you are receiving a value from the function, which can then be used outside of it.

Miguel Nunez
Miguel Nunez
3,266 Points

Wow its making more sense now so if I did some math in a php function and I wanted the answer to be revealed to me I will have to use the return code to get my answer? Like for example 1+2 and I want my answer then I will use the return code to give me this right = 3 ? I know that's not php example but i'm trying to make that math example easy for me to understand what your really trying to say in easier terms for me to understand.