3

Possible Duplicate:
What's an actual use of variable variables?

i saw the concept of variable variables in php . that is a variable whose name is contained in another variable .

like this

$name = ’foo’;
$$name = ’bar’;
echo $foo;
// Displays ’bar’

one advantage that i see is , you can create variable names with numbers or lettes which you can not use normally . like this

$name = ’123’;
/* 123 is your variable name, this would normally be invalid. */
$$name = ’456’;
// Again, you assign a value
echo ${’123’};
// Finally, using curly braces you can output ’456’

and also you can call some functions like this

function myFunc() {

echo ’myFunc!’;
}
$f = ’myFunc’;
$f(); // will call myFunc();

and in a book i saw this

Variable variables are a very powerful tool, and should be used with extreme care, not only because they can make your code difficult to understand and document, but also because their improper use can lead to some significant security issues.

The problem i have is if using variable variables in the code can be that dangerous . why are we using it . and is there any major advantages using variable variables in my code , if any what are those advantages .

Community
  • 1
  • 1
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193

5 Answers5

1

you can use it in case of not-user-input. Look

function getVar($gid){
    $name = "gid".$gid;
    global $$name;
    $var = $$name;
    return $var;
}

It's useful when you have a lot of variables (same start with ending number, etc...) which is probably save

genesis
  • 50,477
  • 20
  • 96
  • 125
1

Look at your example:

function myFunc() {
echo ’myFunc!’;
}
$f = ’myFunc’;
$f(); // will call myFunc(); 

It is powerful: $f can have a value dynamically and the function called based on this value.

At the same time: If the user was given limitless access to $f this could be a security threat

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
1

In some MVC Frameworks they use it to run a function which will vary depending on the URL:

http://www.domain.com/thecontroller/theaction

in PHP side they will parse the url, get the second segment which is the name of the function then run it, but how? they will assign to a variable like what you have mentioned:

$toRun = 'theaction';
$toRun();
kazinix
  • 28,987
  • 33
  • 107
  • 157
0

I've used double and even treble indirect pointers in C, but have never explicitly used variable variables in PHP (but I have used variable functions and classes).

I think it's somehow reassuring that there's more functionality in PHP than I use - and that I can make informed choices about which constructs I do use (I often use explicit references for example).

symcbean
  • 47,736
  • 6
  • 59
  • 94
0

A nice usage I saw in some framework is using them to access, easier, values passedfrom another script as an array:

$array['test'];
$array['other'];
$array['third_index'];
// or simply $array = array('test','other','third_index');

foreach($array as $k=>$v)
{
  $$k = $v;
}

So you could then have $test, $other and $third_index being usable as variables. Pretty handy when dealing with views, for example.

Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77