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!

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

Why would you use $func = 'answer'; instead of $func = answer()?

I've been getting along quite nicely, understanding everything here in PHP. I find myself wondering, though, in what case would you need to set $func to equal 'answer' instead of answer() if they will create the same output? I'm not confused by what's going on, only by why it is going on.

3 Answers


Simply put, $func = 'answer' stores the word answer into the variable $func and $func = answer() stores whatever is returned from the function answer. If you store the function's name in a variable, you can then you the variable later with () after it like so: $func(). PHP will take the value stored in the variable $func and turn it into a function with the (). While I haven't used this method in my applications, there are times when you would want to store the names of functions inside of variables and call on them later with the variable name. This may not have answered your question completely, but I hope I've given you enough for you to understand the big difference.


Oliver Williams
Oliver Williams
6,278 Points

I don't understand why it would ever be useful to use variable functions

Edmond Cotterell
Edmond Cotterell
8,920 Points


I thought the same thing at first, but after thinking about it, it may be quite useful e.g. imagine if u have to loop through function calls for some reason.

array_of_functions = array('start','pause','go'); $func ="";

for($count =0; $count < 3; $count++) { $func = array_of_functions[$count]; $func(); }

I know its a silly example, but i hope it gives you an idea on how this could be useful.