Dynamically create a PHP variable name

Thanks to this post “Dynamic variable names in PHP” for enlightening me on how easy this is. There’s a lot of stuff out there about PHP variable variables but if you just want to craft a variable’s name based on some other variables that are already set – this is the way to go.


$var = "FooBar";

$extended_FooBar = "An Extension of FooBar";

echo ${"extended_{$var}"} ;

Basically a regular variable with lots of curly braces and – in this case – not a dot concatenation operator in sight.

So simple when you know how.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *