Variable variables (or their official name – pointed out by Ionut Stan in the comments) or “dynamic variables” (how I like to call them) is the name of a feature of the PHP programming language which allows a programmer to define a variable that has its name given by the content of another variable. Like such:

$foo = 'bar';
/*
 * declare a variable called bar into the
 * current scope and initialise it with Hello world
 */
$$foo = 'Hello world';

echo $bar; // will echo Hello world

If find this really useless and very annoying, because a typo such as a double dollar sign in front of a variable can result in some very hard to track bugs. But this isn’t just another strange feature that made its way into the trunk. This is very, very well implemented and some programmers strived a lot to make it work under all possible circumstances. It works so well that you can use strings that cannot be normally used to represent variable names, such as !@#$%^&*()_+-=/*. Really, it works!

$foo = '!@#$%^&*()_+-=/*';
$$foo = 'Hello world';

$variables = get_defined_vars();
echo $variables['!@#$%^&*()_+-=/*'];
/**
 * will echo Hello world - the content of a variable called !@#$%^&*()_+-=/*
 */

Further more, it even works with objects:

class Example {
     /**
      * sample attribute
      *
      * @var string
      */
     private $value;

     /**
      * default constructor
      *
      * @param $_value
      */
     public function __construct( $_value ) {
          $this->value = (string) $_value;
     }

     /**
      * returns a string representation
      * of this object
      *
      * @return string
      */
     public function __toString() {
          return $this->value;
     }
}

$foo = new Example( 'bar' );
$$foo = 'Hello world';

echo $bar; // will echo Hello world

I think this one of those features reserved for the most gurus of the gurus, because after 5 years experience with PHP and a Zend Certification, I haven’t yet grasped the logic behind this…