Perl like temporary variables in PHP
When writing code in the global scope, I often have a problem where I’m overwriting a variable. This happens even more often when I work on code of somebody else. Usualy has the variable which does the overwriting is usualy just a temporary variable.
It’s difficult to choose a general name for all temporary, since you want a short name, but others might have used it for other purposes. However with ${’…’}, you can use any string as variable name, even an empty string of a string with spaces. Numbers get automatically cast to a string. So I nice way of solving the problem could be:
${0} = fwrite($fh, 'Test'); if (!${0}) throw new Exception("Writing the file has failed"); if ((${0} = pathinfo('/var/www/index.html')) && ${0}['extension'] === 'html') echo "Yes boys and girls"; }
02 Nov 2007 Arnold Daniels





Of course (as you allude to by specifying “global”), the proper way of preventing variable naming conflicts is to use functions/objects.
Personally, I would stay away from the syntax, simply because it’s esoteric and one day the PHP team may change this behavior.
Why write in the global scope in the first place? This doesn’t seem to make your code more maintainable at all..
Sure, it’s good to have a model in most cases and use functions in other cases, but please don’t take this overboard and start writing functions for the sake of writing functions.
Personally I don’t like the MVC stategy at all, and with me I think a lot of others. I like a KISS principle. The requested page gets the necessary data, either from a model or some other method. The rest of the page functions as the template. With this method you still be perfectly managable in most projects. Using MVC just gives you a lot of overhead, which for most projects makes it far less managable, because you have a lot more code and a lot more files. Also, when you jump from A to X to W to B, its a lot more difficult for others to work with your code and for yourself to find bugs.
In most cases this means that there is some code in the global scope and this solutions will work nicely. You can’t write programs based on which functionality might dissapear in the future. It has suggested on the internals list to change this behaviour once, only to replied by a big NO.
Also I often work on projects (like open source), which are not written by me. In the old days I would find me compelled to change a lot of code, because it wasn’t pretty. Lucky those days are over.
This only works until someone else uses the same technique with their code in the global scope.
Better to not use globals at all.
Hello, Arnold.
I’ve wrote a few more words about the subject
http://www.alexatnet.com/node/100
Your feedback is welcome!
Looks like you’re taking advantage of a dark corner of PHP there. I wouldn’t be surprised if a future version of PHP stops supporting this.
The discussion of stopping it has been raised on the internals list, but there was a huge voice against it.
Using variable variables and functions is simply the fasted way (and cleanest in my opinion) to make flexible PHP apps. It is much faster than all the new magic, PHP has come up with. This was again confirmed by Larry Garfield.
Ummm, just prefix the name with _tmp_… Much accessible and obvious. BTW, the ${} syntax isn’t obscure or esoteric, it’s simply the how you build variable names dynamically. It may well be less well known, but that doesn’t mean it willl be going away.
thats a funky syntax you got there.
I’d agree it might get deprecated in the next releases…
Victor
Kosher Brandy
It is interesting idea, but it can make script hard to understand for other people.
PHP coder
I found this website by mistake, but you sure have a lot of useful PHP tips here. Great work Arnold! I’m gonna look for your code
Antje Schippmann