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'

?>

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Similar Entries
Multiple Select Drop Down Menu in PHP
It's very easy to take what we did back in January for reusable PHP drop down menu code and make it work for multi value select elements.
Special Characters in Custom CMSs and Forms
Encoding special characters can be tricky, especially when you have clients with custom CMSs who like to copy and paste all the em dashes and trademark symbols from word into your web based text area. Now I have found a solution for my needs!
From Query String to Cookie with JavaScript
Take a query string and save the values to a cookie using JavaScript. A perfect solution for tracking query string's values throughout a site that isn't using any server side scripting.
Swapping Two Variables in One Line With PHP
Swap two variables in one line without using a temporary variable. The all-time popular interview or "I'm more programmer than you" question: swapping two variables without using a temporary variables.
Path and Directory Function in PHP
Some great handy PHP functions for dissecting file paths and getting full paths for how they relate to the file you are in. Never again shall we explode or split a string on / or .!
Next Post
Block Content and Detect Wordpress Preview
Previous Post
Evolving Styles: Add, Change Remove Classes with Prototype

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!