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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.