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'

?>

This site runs on the Thesis WordPress Theme

Thesis Theme thumbnail

If you're someone who doesn't understand a lot of PHP, HTML, or CSS, Thesis will give you a ton of functionality without having to alter any code. For the advanced, Thesis has incredible customization possibilities via extensive hooks and filters. And with so many design options, you can use the template over and over and never have it look like the same site.

If you're more familiar with how websites work, you can use the fantastic Thesis User's Guide and world-class support forums to make more professional customizations than you ever thought possible. The theme is not only highly customizable, but it allows me to build sites with a much more targeted focus on monetization than ever before. You can find out more about Thesis below:

{ 8 comments }

1 blah March 15, 2010 at 5:10 pm
echo "value of $blah: {$$blah}";
2 shiben November 2, 2010 at 5:57 pm

It should be noted that if you use multiple assignment on one line to assign an object, the object is assigned by reference. Therefore, if you change the value of the object’s property using either variable, the value essentially changes in both. For example:

class MyTest {
    public $var = 'hi';
}
$var1 = $var2 = new MyTest;
echo("$var1-&gt;var $var2-&gt;var"); //outputs: hi hi
$var1-&gt;var = 'bye';
echo("$var1-&gt;var $var2-&gt;var"); //outputs: bye bye
3 Ibrahim December 5, 2010 at 7:55 pm

i have several combo boxes (about 15) that displays same array of data selected from a mysql database, how do i pass the same array of the data without selecting from the database for each combo box to speed up the page load.

4 Sarah March 5, 2011 at 11:24 am

Loved the girly variable names :) Thanks for this, had to use this on my website.

5 David Eldridge January 13, 2012 at 10:56 am

Thanks for the article. It was very helpful. (I think I’d heard of both of those conventions: $$variablename and $something = $somethingelse = ‘yet another thing’.) But I had never really gotten it until today. I appreciate the ‘girly’ variable names, too, for the same reason you stated.

6 Alex Gray January 21, 2012 at 6:00 pm

You fail to mention why someone might WANT to do this…. After all, if you can just use the original variable, why not? What is the benefit of the double abstraction?

7 David Eldridge January 21, 2012 at 8:17 pm

I would not have been able to answer a couple of days ago, but I began using the practice Friday, and it is nice for reusing code easily, (probably even recursively).

8 Terri Ann
Twitter:
January 23, 2012 at 12:03 pm

Alex -
Sometimes you don’t have easy access to the name/value of a variable or you want to use a value that is dynamically populated from a feed, API or database. That’s when you might need to use something like this.

I post solutions, not problems. There’s a million different problems or ways that you might need to implement this solution. I had to learn how to do this when a database returned a large set of meta data and I needed information within that metadata to make another query. I you can’t come up with a case or a time this would have helped you then just tuck it into the back corner of your brain. While it’s not a commonly needed solution there will come a time you’ll want to know how to use the value of variable as the variable name some day!

Comments on this entry are closed.

Previous post:

Next post: