Variable variables let you dynamically access variables by their name. Sound confusing? This article will explain PHP variable variables, and provide code examples.
What is a variable?
In computer programming, variables store a value, or reference to a value, for later use. Variables have names, and when you access a named variable, the value it contains can be read or updated.
Declaring variables in PHP
In PHP, a variable is declared when it is first used:
$myVariable = "Hello LinuxScrew";
Above, a variable named $myVariable is declared using the = (equals) operator, and assigned the string value Hello LinuxScrew. Once a variable has been declared, it can be used in any following code.
Variables can also be declared without a value:
$myVariable;
Variable names in PHP
There are several rules that you must follow when naming variables in PHP:
- Variable names must begin with a $ (dollar sign symbol)
- Variable names may only contain alphanumeric characters (A-Z, 0-9) and underscores
- Variables cannot be named $this – a reserved variable name that has specific uses
Variable variable names
You can use a variable as a variable name – creating variable variables. This sounds confusing – but makes sense in practice – so here’s some code to demonstrate:
$variableName = "myVariable"; $$variableName = "Hello LinuxScrew"; echo $myVariable; // Will print "Hello LinuxScrew"
Above, the first variable defined contains the name of a future variable. That variable is then created. You can spot the variable variable by the $$ (double dollar sign) – the name of the second variable is taken from the value of the first variable.
But why?
This is more than a neat trick, as it adds a lot of flexibility to how you can structure your code, and has has practical applications. For example:
Without variable variables, if we want to give each item in an associated array its on variable, each one must be typed out:
$myArray = [ "name" => "Fred", "email" => "[email protected]", "phone" => "55500000", ]; $name = $myArray["name"]; $email = $myArray["email"]; $phone = $myArray["phone"];
With variable variables, the array can be enumerated, and the key of each array item can be used to create a new variable, using the value of the key variable as the name:
foreach( $myArray as $key => $value ){ $$key = $value; }
Another example, variable variables can simplify the process of setting the values of object properties.
Without variable variables, each object property that needs to be set from the array again needs to be typed out:
$contact = new stdClass(); // Creates an object $contact->name = $myArray['name']; $contact->email = $myArray['email']; $contact->phone = $myArray['phone'];
With variable variables, the name of the object property can be set from the array key:
foreach( $myArray as $key => $val ){ $contact->$key = sanitize_values($val); }
Above, the $key in $contact->$key is the variable variable – it is being used to access a variable object property.
Variable variables can also be used to access array members, but there are some extra precautions to be taken.