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

Why a variable function?


I am super-noob to PHP so be kind.

my question: What is the purpose of a variable function?

5 Answers

Kevin Korte
Kevin Korte
28,148 Points

Kinda sorta.. to clarify first, you have to call $somevariable = 'function' as the order of this matters. Also realize function in that example would be the name of your function.

Than you can use $somevariable() to call the function it was assigned to. The big help here is not changing the the code inside the function, as you could not use a variable function, and instead just reference the function in your code, and if you changed the code in the function, all the references to that function will also now use the new code. What I was trying to show, is when you need to change the references to a whole new function. Let's say I want to keep my first function, but I need some code to reference a new function, which I'll call function2. I write the new function, and change ``$somevariable = 'function2' so now when my code runs, it all runs on function2, thus allowing me to change that in simply one spot.

So essentially, the point is to be able to keep your old function, while creating a new function and only needing to change where it is referenced once? The other simple option, of course, would be to just change the code within the old function, essentially making it a new function - which would negate the need to use a variable function right? The downside being that you would not be able to keep the old function for future use? You could also just comment out the old code within the one function, while adding the new code, this would allow you to save your old function code just in case for later, while making your new code operational.

Kevin Korte
Kevin Korte
28,148 Points

It makes the code, just that much modular and D.R.Y (don't repeat yourself). Which as an application grows, becomes quite important.

Here is probably a poor example, but lets say I have a function to calculate the amount of tax that someone owes me. Now lets say I use that function no matter which state the person is in, they owe me the same amount of tax, but I have a different function to calculate the shipping charges.

At this stage, I can have my two functions tax and shipping next to each other, and referred to later in the code. But now tax laws have changed for some states, and I have to adapt for that. So now what I can do, is write a new function next to the existing functions that take into account the new tax requirements, and instead of going through my code and replacing each and every function call to my old tax function for each state, I just simply reassign the variable the function references, and once I do that, the code should work fine. Doesn't matter if I referenced the function 23 times to calculate the tax for a user of that state, I can just make one change, and all 23 function calls get updated to use the new, correct function.

Again, that's probably not the best example, but the idea is that you only want to have to set something, somewhere, one time. This makes it far more easy and less error prone to make changes to the code later. Hope that helps.

Daniel Fitzhugh
Daniel Fitzhugh
9,715 Points

Thanks Kevin for this explanation ! After reading it I understood immediately how variable functions work. I wish the video could have explained things better.

The PHP videos don't ever present use cases for why they are teaching you things.... they just spend the entire video writing code without explaining why. I feel like they probably spent a lot more time and effort on the javascript courses, where things are explained and illustrated in a much more comprehensible way....

Yohan Park
Yohan Park
7,148 Points

Kevin Korte , thanks for your explanation. In your example:

function tax(){return tax owed};
$tax2 = 'tax';
$tax2() + updated tax

but then why not juse use

$tax2 = tax() + updated tax

What's the purpose?

AHHH. Wow. Ok, I think I am beginning to understand this. Thank you.

So far, thanks to your example, I understand that I can set a function = $somevariable and then use that variable to call the function like this:


and this will call the function.

$somevariable is defined higher up in my code and all I would need to do is change the code inside of the "{ code here }" part of the function and then the calculations would reflect that one change I set equal to $somevariable because I am calling the variable function instead using the same function over and over and having to change it wherever it may be in the code.

Is that right?

Ah, that is making sense now. Thank you. Making the variable function equal to the new variable function you made so that function gets called instead of the old one.