Reputation: 1104
@var = 'foo'
@foo = 'bar'
@@var
gives you 'bar'
?
What's the use of this technique?
Upvotes: 4
Views: 373
Reputation: 420
It allows you to change which variable is called dynamically. I use this feature in PHP with double-dollar-sign $$.
$var = 'bar';
$bar = 'hello';
echo $$var; // Output is hello
You can think about it as $($var), where it evaluates the inner expression then evaluates the outer with the result of the inner. I once used this in a sanitation of user input.
// In a controller
for(array('email', 'firstname', 'lastname') as $input){
$$input = $_POST[$input]; //Will create a variable with that name
sanitation_function( $$input ); //This will sanitate the input
}
Upvotes: 4