+ 2
What is the rational behind the variable variable ability in php?
3 Answers
+ 4
I think it's pretty cool actually
$a = "s";
$c = "a";
echo $c;
+ 2
+ 1
I can only guess. As with a lot of PHP's features it's probably "because other languages had it" when PHP came out. PHP is a kitchen sink full of bad features.
I distinctly remember that variable variables were very common in TI-Basic, because a lot of data you worked with was strings, and managing arrays was a bit of a hassle. So building strings like "v1", "v2", "v3", and using variable variables to use them as variable names was not only easiest, but also shortest (which is important when programming on a calculator!)
I think bash and perl have it too.
As for PHP? It probably should have never been a thing. PHP wanted to imitate C, so maybe they thought PHP needed pointers, but pointers make no sense in PHP so they made variable variables instead.
Maybe there even was a use-case way back when (PHP gained a lot of features over the years), but there most definitely isn't one now.
Bottom line: Never use variable variables!