Variable variables in PHP

Posted by andy gaskell on Apr 1st, 2006

While learning PHP I came across a feature that’s completely new to me – variable variables. With variable variables you can dynamically assign variable names and reference those dynamic names. I understand how they work, I’m just not sure how useful they’d be. I suppose an array of variable variables could pass as a poor man’s Hashtable or NameValueCollection.

Here’s some code that demonstrates variable variables:

<?php
     $recipe = "beans";
     $$recipe = "& big ideas";

     echo("$recipe $beans");
     echo("<br/>");
     echo("$recipe ${$recipe}");
?>

Here’s the output:

beans & big ideas
beans & big ideas

Technorati Tags: ,