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 PHP Variable Functions

Miguel Nunez
Miguel Nunez
3,266 Points

Don't say return returns a value which is very confusing but 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.

2 Answers

Kristian Egebæk-Carlsen
Kristian Egebæk-Carlsen
9,809 Points

Because the point of many functions is not to echo something, but merely return some variable, object or array which you have to use in other parts of you code.

Aaron Coursolle
Aaron Coursolle
18,014 Points

Think about magic tricks.

An echo is like the "ta-da" reveal at the end of the trick.

Return is like quietly moving your coin from one hand to the other so that it later will "show up" in someone's ear.

When learning the steps of coding, we may add extra echo statements for our benefit. They help us know if all the steps are working. But we only do a "ta-da" in the real world when we want to make the audience applaud, or perform an "echo" when the user needs to know something. Otherwise they would get bored or confused with too much information being presented at once.