PHP Variable Names: Curly Brace Madness
Posted on January 12th, 2008 in PHP |
Most PHP coders will know that one is able to specify variable names in the following format, generally within double-quoted strings, however much they from on it:
$str = "Hello ${username}, you look nice today.";
However, that's not the only way to use the brace syntax!
Straying away from that common usage, it is perfectly reasonable to use the same format in a normal assignment operation:
// Same as $str = 'Hello Peter'; ${'str'} = 'Hello Peter';
Now, moving into the bizzare, the string value within the curly braces can be any expression which results in a valid variable name (and more, see later)!
// Each are the same as $str = 'Hello Peter'; ${'s'.'t'.'r'} = 'Hello Peter'; ${substr('string', 0, 3)} = 'Hello Peter'; ${ 'a' == 'b' ? 'rts' : 'str' } = 'Hello Peter';
Finally, lets get really crazy. The strings within the curly braces needn't necessarily strictly adhere to the normal variable naming conventions! Since the curly braces literally mean "evaluate my contents as a string", we can play around even more!
// You can't access these with 'normal' global variables, // but the curly brace form works as does getting at them // via the $GLOBALS superglobal variable! // Values are converted to strings which is why 0xFF => '255', etc. ${007} = 'Hello Peter'; // $GLOBALS['7'] ${0xFF} = 'Hello Peter'; // $GLOBALS['255'] ${2 + 2} = 'Hello Peter'; // $GLOBALS['4'] ${'Hello Peter'} = 'Hello Peter'; // $GLOBALS['Hello Peter'] ${'cats, pet food, dogs'} = 'Hello Peter'; // $GLOBALS['cats, pet food, dogs']
I'm pretty sure these weird variable names serve absolutely no practical purpose, but it's interesting to know of little quirks like this (well, interesting for weirdos like me). Have a play around yourself, remembering that you can print_r() or var_dump() the superglobal $GLOBALS or even use get_defined_vars() to find out the variable names that have been created.


6 Responses
"Have a play around yourself, remembering that you can print_r() or var_dump() the superglobal $GLOBALS or even use get_defined_vars() to find out the variable names that have been created."
Or use your favourite PHP IDE and use the almighty debug prespective.
I meant to say perspective.
Absolutely, if you use an IDE then it makes sense to use any available features within the application! Personally, I'm still a little behind the times using a "text editor" (Coda) and don't have the luxury of built-in debugging, profiling and other lovely features that a full-on IDE would have.
If someone built a light-weight IDE (ie, it worked just like TextMate or Code) with the ability to (optionally!) make use of heavier-weight features, that'd be lovely and, in that case, could the someone let me know about it!
Almost all the IDE's are heavy. It depends on what your prefer. All IDE's can be customized very easily.
I use Eclipse PDT http://www.eclipse.org/pdt/index.php.
I have tried to use a whole slew of IDEs in the past and there's really nothing exceptionally wrong with them. I rather prefer almost just a 'window' with the code in (pretty highlighted) and nothing much else.
I remember trying Aptana, Zend, Eclipse, Komodo, PhpED, PHP Designer, PHP Edit--those are the ones I can name off the top of my head--but it was nearly always simply the 'bulk' of such applications which put me off. I'm a Zen-like person and they all just made too much noise for my liking!
Well,
I have a code, what worked in PHP 4.x but didn!t in PHP5.
The code uses the function you mentioned above.
with PHP5 the
print_r(${('_GET')});
line prints the global? $_GET array, while
print_r(${('_G'.'ET')});
gives an "Undefined variable: $_GET" php error.