2

what does two back to back $ behind a variable means. Like this $$id

where can I find more information on that Thanks

Your Common Sense
  • 152,517
  • 33
  • 193
  • 313
Asim Zaidi
  • 23,590
  • 46
  • 125
  • 213

3 Answers3

10

In PHP, $$ means you are about to inflict years of pain and suffering on at least one maintenance programmer. Note that you might wind up being that maintenance programmer.

It is a variable variable. Imagine this:

$quux = 'bar';
$foo[$quux] = "baz";
echo $foo['bar']; //prints baz

if there was no such thing as arrays, you might try something like this:

$quux = 'bar';
$$quux = "baz";
echo $bar; //prints baz

luckily we do have arrays so please don't use variable variables unless you are doing something convoluted and magical* and have no other choice.

*: Please don't do convoluted magical things, either.

Carson Myers
  • 34,352
  • 35
  • 118
  • 164
  • You can make anything look convoluted with undescriptive variable names like that. Variable variables are just as maintainable as regular ones when named properly. – Lotus Notes May 06 '10 at 06:20
  • the foo, bar, baz, etc variables are very common when demonstrating a language feature. Obviously proper use of naming is a must in any language, but I'm talking about the maintainability of variable variables, not of dummy names -- I think they take much effort to use in a way that is not evil, so I'm okay with presenting them in a way that would make a novice programmer approach them with caution. – Carson Myers May 06 '10 at 08:36
6

These are called variable variables.

$foo = 'bar';
$id = 'foo';

echo $id;  // prints foo
echo $$id; // prints bar
codaddict
  • 410,890
  • 80
  • 476
  • 515
2

in the PHP manual of course

http://www.php.net/manual/en/language.variables.variable.php

note that it's obsolete and senseless syntax and you should always use arrays instead.

Your Common Sense
  • 152,517
  • 33
  • 193
  • 313