Home arrow PHP arrow Page 4 - Using Variable Variables in PHP

Variable Variables with Functions - PHP

Quite possibly one of PHP’s least utilized yet most valuable features is its support for Variable Variables. In short these are a method of using the value of one variable to call the name of another. Sound confusing? We'll explain everything...

TABLE OF CONTENTS:
  1. Using Variable Variables in PHP
  2. Variable Variables?
  3. Using Variable Variables with Arrays
  4. Variable Variables with Functions
  5. Variable Variables with Classes
  6. The Never-ending Variable
  7. Conclusion
By: Eric Seufert
Rating: starstarstarstarstar / 15
February 14, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
A great yet widely unused element of variable variables is their ability to be employed with functions. The function name can be set within a variable and then run through calling the variable variable of that variable. Here’s an example:

$function1 = "sort_stuff"; ${$function1()}; #this sets the result of the sort_stuff() function into a variable ${$function1($var1, $var2, $var3)}; #this does the same thing but sends the function parameters

Here’s a quick example of this in action, building on the same program segment I had introduced variable variables with:

$x = "Eric"; $$x = "Seufert"; $function = "value"; echo "${$function()}"; function value() { return "Eric"; }

This code would output:

Seufert

as the echo statement is printing the value of the variable that gets created from the resultant of the value() function, "Eric". As we have already set $$x (which, following the variable variable, is the same as $Eric) to "Seufert", the same value is outputted, only this time resulting from a function call.



 
 
>>> More PHP Articles          >>> More By Eric Seufert
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: