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

Riley Hiltz
Riley Hiltz
11,653 Points

Understanding Variable Functions

Could you confirm my understanding and perhaps expand it?

My code:

function answer() {
  return 42;
}

function answer1() {
  return 49;
}

// lets say I created answer function and use it. For loop perhaps in code somewhere.

// Original function
$answer = 'answer';

/* 
Old function no longer good, so I mde new function, I don't want to go through code to change to call new function...
*/

// New reference to new function
$answer = 'answer1';


for ($i = 0; $i < 10; $i++) {
  echo $answer() . "\n";
}

Do I have to always call functions with $func? For example, if instead of $answer information loop, I simply use answer(), will I be able to do anything? If I remove $, It will refer to old function.

Do this mean I should make habit of using variable functions? Or am I missing something?

Thanks

Bill Dowd
Bill Dowd
7,681 Points

You can call the function by simply using answer() or answer1(). But what you've done is used a variable $answer to hold the function name. If you choose to do that then yes, you have to use $answer. The question is why would you want to use a variable to hold the function name? How about using the function to actually do something like add by passing it a value or two. For example:

function answer($a, $b) {
    return $a + $b;
}

Then call the function like this:

echo answer(10, 20);

Or for more fun, use your loop to change one of the numbers like this:

for ($i = 0; $i < 10; $i++) {
  echo answer(10, $i) . "<br />\n";
}

Does that make sense or am I missing your question? Bill

Riley Hiltz
Riley Hiltz
11,653 Points

Hi Bill Dowd,

I had trouble trying to see the point of variable function. Did you watch video related to this post?

See part of code

$answer = 'answer';

I know I can simply replace answer() to answer1() in the for loop. I thought the point of variable functions is that in situation where multiple calls were made with answer(), instead of replacing every of them to answer1(), I could just reassign reference to new function... Then I realized I still have to change to $answer, anyway (instead of answer()).

Hope I make sense here...

1 Answer

Hey Riley - Not sure if you're still looking for some clarification on this. One of the nice things about PHP is that it supports this kind of dynamic programming. The reason you would use a 'dynamic' function is so that the for loop will always 'just work', since the for loop is just calling the function using whatever function name is stored in the $answer variable. You might have some conditional logic before the for loop that will determine what needs to happen in the for loop, but as long the $answer variable is assigned to a function name everything will continue to work.

This is not needed in every situation only when you need the function call to be dynamic. This kind of programming is not always straight forward so if you're still a little confused, think of this like eye glasses, you'll know when you need it.

Happy Coding

_Ben