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 = "</pre>
<h2>An Extension of FooBar</h2>
<pre>
";

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.

  1. No comments yet.

You must be logged in to post a comment.