0

What does the double $ statement stands for in PHP?

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
mck89
  • 18,918
  • 16
  • 89
  • 106

2 Answers2

12

It means a variable variable:

$a = 'b';
$b = 'test';
print $$a; // test

For the most part (although there are exceptions if you know what you're doing) they are bad practice and whenever you see someone using them arrays are probably the better idea.

Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • You're correct, but how about an example for the newbies of why you'd use it? IIRC, it was largely for superglobals, etc, so most won't know when or why to use it, let alone what it is for. – Robert K Aug 25 '09 at 15:09
  • 1
    @the wicked flea: If you need or want variable variables, something is wrong with your code. Scrap it and rethink the problem. Variable variables is usually fixed with arrays and dividing your code into classes/functions. – OIS Aug 25 '09 at 15:13
  • I was going to post an example where variable variables might come in handy. Couldn't find one that could not be implemented more elegantly without, though :) – jensgram Aug 25 '09 at 16:01
  • I know that CakePHP internally uses some variable variables to handle its model creation. It's a lot of edge cases to be sure, but I wouldn't say they are NEVER needed, certainly not unless you know what you are doing. – Paolo Bergantino Aug 25 '09 at 16:57
7

It means "The author should be using an associative array".

(It is a variable variable)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335