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

Omer Asadullah
Omer Asadullah
10,415 Points

Returning Values - Need explanation in comprehensive language

In the course PHP Functions there is a video called Returning Values. The teacher's name is Hampton Paulk. I can't understand the reason behind using return. I would suggest an easy to understand explanation strategy, giving examples from day to day life experiences and use analogical statements to make difficult concepts more easy to understand. Other teachers like Nick Pettit and that CSS guy use this method and make things understandable.

PS: No offense to Mr. Hampton

As an example would suggest, readers visit this link. http://stackoverflow.com/questions/9387765/what-is-the-difference-between-php-echo-and-php-return-in-plain-english

Yonatan Arbel
Yonatan Arbel
7,055 Points

the example in StackOverflow is hilarious.

as a C# teacher i agree that bringing examples form real life make things more understandable. anyhow, each teacher and his method of teaching.. i guess it's something that cannot be changed if the teacher doesn't uses this style of teaching.

anyway, the student should adjust himself to the teacher because there are many students that each one prefer a different style of teaching :)

good luck buddy!

4 Answers

To put it very simply, 'echo' will print something to the page, 'return' won't.

You've mentioned that you're interested in Wordpress, so let's take an example from there - it's a slightly more code based example.

When referring to (for example) blog posts, Wordpress has two functions to get data from the blog post get_post and the_post.

There's a very subtle difference between these two functions - the_post will ECHO (print) the whole post to the page. You have no control over the data and you cannot manipulate it on the page. You're kinda stuck with what your theme gives you.

But let's say you need to play with the blog post data before you display it. Something like merge the title and content body into the same html tags or change the the layout for a particular page. get_post will RETURN the post to you as an array, and won't print it to the screen. Because you've now got the raw data, you have more flexibility.

I apologise if you still find this a little complicated, but it is a real world example ;)

To understand this better, I would practice using it. You could make a mini-calculator.

One function could do some adding:

<?php

function add_numbers($number1, $number2) 
{
    return $number1 + $number2;
}

Another function could do displaying:

<?php

function add_and_display($number1, $number2)
{
    echo add_numbers($number1, $number2);
}

Using return generally gives you more freedom. E.g. say your building a web app that's based on adding two numbers, but you don't always want to display the result. If you used 'echo' in add_numbers, you would be stuck! This way, you're separating things out so you can reuse code.

You could do things like this without random numbers appearing through the calculation!

<?php

echo add_numbers(2, 2) + divide_numbers(10, 2); // would print 9

its not a good practices to have echo in a function in real life because you would reuse function more than once.

say you had 2 function

<?php
function get_first_name(){
     return $first;
}
function get_last_name(){
     return $last;
}?>

later you can reuse

<?php
if(get_first_name()=="Omer")
{
$user="authenticated";
echo get_first_name() . " " . get_last_name();
}
?>

you should have it separated.

David Valentino can you share the source where you learned echoing in functions is bad practice?

Don't get me wrong, the example you give is totally valid and a good use case for not wanting to echo inside a function or method. But as far as I know, it's not bad practice. It's perfectly valid for a function to 'print' and 'echo' things.

Tom Cawthorn, i am sorry i am mistaken to not write completely. i think you're right. my teacher taught me that cant put echo in specified method like set and get but we could in displayData() method because she said its bad to put echo in get and set because we are going to reuse it for different reasons

What i understood, better is to use 99.9% RETURN function instead of echo, inside the functions.