Multiple and Dynamic Variable Assignment in PHP
Variable assignment in PHP is one of the first things you learn when you begin.
It's easy enough: $variable = 'value';
Well there's other ways to assign value to variables that might come in handy too.
Multiple Variable Assignment on One Line
It's really easy to give two variables the same value on one line by setting $v1 = $v2 = 'value'
Excuse my girlieness in these examples using shoes, dresses and handbags as variable names but for me their more easily and logically distinguishable than foo and bar because I can develop a visual understanding as well as the code understanding this way.
And also this first example is my outfit for NYE208...so cute!
Here's a quick sample:
PHP
<?php
$dress = 'purple';
$shoes = $handbag = 'silver';
echo "dress: $dress\n"; // displays 'dress: purple'
echo "handbag: $handbag\n"; // displays 'handbag: silver'
echo "shoes: $shoes\n"; // displays 'shoes: silver'
?>
Both $shoes and $handbag are set to 'silver' in one convenient line!
Dynamic Variable Names or Variable Variable Names
Building off the above example I'm now going to create a new variable that is equal to one of the current variable names.
PHP
$favorite_accessory = 'handbag';
Now with the double dollar sign ($$) I can reference $handbag's value of 'silver' by using $$favorite_accessory
PHP
<?php
$shoes = $handbag = 'silver';
$favorite_accessory = 'handbag';
echo "favorite: $favorite_accessory\n"; // displays 'favorite: handbag'
echo "favorite's value: " . $$favorite_accessory . "\n"; // displays 'favorite's value: silver'
// NOTE: The following line doesn't output correctly as seen in the comment
echo "favorite's value: $$favorite_accessory\n"; // displays 'favorite's value: $handbag'
// that line displays just like: echo "favorite's value: \$handbag\n"; actually printing the $ symbol
?>
So since the $$ cannot be used within the quoted string ("") it must be concatenate by closing the string and using the . to concatenate the dynamic variable.
There is a way to use the $$'s ability within a double quoted string: by using the {} special syntax.
PHP
<?php
echo "favorite's value: ${$favorite_accessory}\n"; // displays 'favorite's value: silver'
?>
This special syntax is also the way to use an array index's value in a quoted string.
PHP
<?php
echo "var array, sub 2: {$var[2]}\n";
?>
Assigning Values Using Dynamic Variables
So now we know you can access values using dynamic variable, but you can also assign value using the dynamic variable, double dollar sign ($$).
So building off the above examples we know that $$favorite_accessory is dynamically equivalent to $handbag so setting $$favorite_accessory to be 'green' should set $handbag to 'green'.
In the following example, it does just that:
PHP
<?php
$dress = 'purple';
$shoes = $handbag = 'silver';
echo "dress: $dress\n"; // displays 'dress: purple'
echo "handbag: $handbag\n"; // displays 'handbag: silver'
echo "shoes: $shoes\n"; // displays 'shoes: silver'
$favorite_accessory = 'handbag';
$$favorite_accessory = 'green'; // equivalent to $handbag = 'green';
echo "dress: $dress\n"; // displays 'dress: purple'
echo "handbag: $handbag\n"; // displays 'handbag: green'
echo "shoes: $shoes\n"; // displays 'shoes: silver'
?>
the newest discoveries, stories and shared tips!Come on, all the cool kids are doing it ;)


