Variable Variables

One thing that I've found incredibly useful in some of my form-based applications is variable variables. Variable variables allow you to have variable names for variables. This way you can generate variable names on the fly for whatever purposes your script requires.

Example:

$name =  "integer"; 
$$name = 10;
//results in $integer = 10;

Note that when you echo these values to the screen, you get different results from those which you may expect. In the above example, if you want to print the value out to the screen, you cannot enclose the variable variable name in quotes as you might normally:

 echo "<p>$$name"; // results in "$integer" being printed to the screen echo "<p>" . $$name; //results in "10" being printed to the ...

Get Advanced PHP for Web Professionals now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.