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 trialMiguel Nunez
3,266 PointsWhat it return in php functions?
In simple terms with out using fancy words with your best approach to explain this as easy as possible what is a return in PHP functions. I have basic understanding in arguments and functions when I think of a return I think of it as a non-coding relation for example John borrows a ball from Tom days later John returns the ball to Tom is it that simple like that or is the code sense different than the English word return why would any one wants to use a return and what is it design to do?
4 Answers
robbertdekock
13,390 PointsI've found an explanation of the differences with return on this page.
I've set up some code to support this.
<?php
function function_one() {
echo "Value1";
}
function function_two() {
return "Value2";
}
$function1 = function_one();
$function2 = function_two();
var_dump($function1);
var_dump($function2);
?>
Hope this helps...
robbertdekock
13,390 PointsHi Miguel,
If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. This allows you to store data from a function directly into a variable.
The most clearest explanation about returns I've found is the video you're referring to. Try reading the video's script instead of watching it. This sometimes works for me.
If you have more questions please ask, and I'll try to explain! :)
Miguel Nunez
3,266 PointsI get confused when every one say return. Do you mean for example like return like I borrowed a ball form you and I returned your ball back to you? or does that word mean something different in the world of coding ? So did some where in the code the code borrowed something that sounds to human like which makes me even more confused visually can you explain what it is with out using phrases for example it return the statement etc.. So if you use return what effects does it have vs with out using it? Still don't understand don't all codes ends execution as your code goes on ??? so confused still I can't find explanation that does not use the key word phrase return example then the value returns what does that mean still what did the other codes borrowed?
Adam Doe
30,840 PointsA return statement will return a value from a function. Here's an example. We have a ball class with a color property set to 'red', and a change_ball_color method that returns a new ball color we provide. The change_ball_color method will return the new ball color of 'blue'. However, we aren't printing anything to the screen until we echo $ball->color. Hopefully this helps.
<?php
class Ball
{
public $color = 'red';
public function change_ball_color($color)
{
$this->color = $color;
return $color;
}
}
$ball = new Ball();
echo $ball->color;
$ball->change_ball_color('blue');
echo $ball->color;
?>
Miguel Nunez
3,266 Pointsthe out post was redblue I thought you said blue?
Adam Doe
30,840 PointsCorrect, I'm showing you that we changed the balls color. There is an echo statement that prints out red. That's where the color red comes from. Then we change the balls color. This won't print anything to the screen. Try playing with the code a little bit and commenting parts out. Then we echo the balls color again, that's where blue comes from. Because I haven't added any spaces in we get redblue. Hope this helps!
Miguel Nunez
3,266 PointsMiguel Nunez
3,266 PointsSorry I already visit that page earlier :( it looks like it does the same thing as echo