note 113496 deleted from language.references.pass by googleguy
| From: | googleguy@php.net | Date: | Sun, 23 Feb 2014 17:26:12 +0000 |
| Subject: | note 113496 deleted from language.references.pass by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198618@lists.php.net to get a copy of this message | ||
Note Submitter: Aycan Yat
----
Sometimes you might wish to modify value of an existing variable by its name. This is easily
accomplishable with a combination of using "passing by reference" and "variable
variables".
$first_var = 1;
$second_var = 2;
$third_var = 3;
$which_one = array_rand('first', 'second', 'third');
//Let's consider the result is "second".
$modifier = $$which_one; //Now $modifier has value 2.
$modifier++; //Now $modifier's value is 3.
echo $second_var; //Prints out 2
//Consider we wish to modify the value of $second_var
$modifier = &$$which_one; //Simply passing by reference
$modifier++; //Now value of $second_var is 3 too.
echo $second_var; //Prints out 3
It's that simple!