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

return value

function get_all_flavors(){
$flavors = array(
        "Jalapeno So Spicy",
        "Avocado Chocolate",
        "Peppermint",
        "Vanilla",
        "Cake Batter",
        "Cookie Dough"
    );
    return $flavors;
}

if I call this function without putting the return, means nothing will happen? the case above does return anything?

2 Answers

It is returning something you just aren't outputting anything, e.g. no echo command. If you want to see the contents of the entire array add in this line print_r(get_all_flavors());

I made a working example for you http://codepad.viper-7.com/zldFEB

Thiago van Dieten
Thiago van Dieten
17,059 Points

Hello,

Without getting too deep on scopes, the $flavors array is only accessible by other functions and variables IN the function get_all_flavors.

Doing this //defining the function get_all_flavors() you've written get_all_flavors(); $sameflavors = $flavors; //no output since $flavors is undefined So assigning $flavors to $sameflavors won't work since it can't access the array.

Now if you try to assign the $sameflavors variable to the function that doesn't return anything. //defining the function get_all_flavors() you've written WITHOUT return get_all_flavors(); $sameflavors = get_all_flavors(); //Also no output

So basically, all the function does is make an array. By returning the $flavors array in the function, get_all_flavors() suddenly sends out the value of $flavors. And then you can finally do things with the data outside of the function, like assigning to a variable.

I'm not really fond on how i've explained this, feel free to correct my mistakes or write something better