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 trialAndrew Phythian
19,747 Points"This works, but it's better with functions like this to return the HTML to the main code as a piece of text instead"
What is the explanation for this line? The HTML that the function was wrapped around was initially echo'd but then replaced by a variable.
2 Answers
Alex Sandro
3,361 PointsI have found a good explanation here. Hope this helps!
<?php
function function_one() {
echo "Value1";
}
function function_two() {
return "Value2";
}
$foo = function_one();
$bar = function_two();
var_dump($foo);
var_dump($bar);
?>
Adam Brown
12,453 PointsI imagine it has to do with clarity in the code as well as reusability.
With the return value, echo displayText();
will run just fine.
but if you don't have a return value, echo displayText()
will create an error.
Furthermore, if you wanted to test the output of displayText() in a conditiional, it would be much easier to compare if the function returned a value.
if (displayText()=="Hello, world!"){echo "You did it!"}
S Ananda
9,474 PointsS Ananda
9,474 PointsThat was a great link. It really helped me to understand it completely. Thanks Alex Sandro.