$literal = "Hello"; $vv = "literal"; echo $$vv; // => prints "Hello"
Are there actually any real use-cases for this language feature?
Assuming a url of
That's a pretty standard practical use of variable variables. Neat trick, but you should be extremely careful when using it, over/abusing it will certainly lead to horribly unmaintainable code.
Disclaimer: The code presented in the answer is only intended to illustrate the use of the feature. It does not cover proper design, security, maintainability, or sanity. Do not use.
|show 3 more comments|
Honestly? In nearly 10 years of PHP experience I've never encountered a problem to which the only solution (or indeed the best solution from a choice) was to use a variable variable. The closest I've got is being able to access constants on an instance of a class such as
I suspect it's one of the many warts that exist in PHP that seemed like a good idea at the time.
So in answer to your question, if there are use cases where it's the best or only approach to a particular problem, I've not seen them.