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 frown on it:
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:
${'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)!
${'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!
// 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.
14 Comments on PHP Variable Names: Curly Brace Madness.
“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.
[...] leaves anyone that looks at it completely bewildered. Who knows, maybe even something sensible. This guy seems to be getting quite excited about [...]
@Peter – Try NetBeans, it is amazing.
I was trying to clean up some strings tonight and discovered that I can use curly braces within double quotes to concatenate a var with the text immediately following it. It looks much much cleaner than using periods and double quotes. For example, if working a dynamic CSS rule:
background-position: $x_offset”.px.” $y_offset”.px.”
vs.
background-position: {$x_offset}px {$y_offset}px.
…a lot more readable.
Thanks
${'s'.'t'.'r'} = 'Hello Peter';Helped a lot…..
after hours of solution searching …
& solving problem
what about:
$bar = 'ho';
${$foo . $bar} = 'Yes!'; // Same as $hiho</code>
[...] Cowburn.info [...]
This also works with object members, where I had to use this recently to deal with a database wrapper:
$r->1st_columnisn’t valid, but
$r->{'1st_column'}works as expected.
Bozo said:
$r->{'1st_column'}Does object ‘members’ include methods? eg. Is this valid syntax:
$this->{$bool ? 'a' : 'b'}($args);I am hoping it is equivalent to:
if ($bool) $this->a($args) else $this->b($args);Bitcoin FAQ, yes that will work as expected.