PHP Variable Names: Curly Brace Madness

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:

$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.

Previous
Next

14 Comments on PHP Variable Names: Curly Brace Madness.

Add your two pennies, others have already donated theirs.

  1. Haris
    Jan 13 2008 at 17:46 BST

    “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.

  2. Haris
    Jan 13 2008 at 17:47 BST

    I meant to say perspective.

  3. Peter
    Jan 13 2008 at 17:56 BST

    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! :)

  4. Haris
    Jan 13 2008 at 18:05 BST

    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.

  5. Peter
    Jan 13 2008 at 18:19 BST

    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!

  6. Atus
    Atus
    Sep 3 2008 at 12:47 BST

    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.

    :(

  7. Aug 19 2009 at 14:42 BST

    [...] leaves anyone that looks at it completely bewildered. Who knows, maybe even something sensible. This guy seems to be getting quite excited about [...]

  8. Nikola
    Nikola
    Aug 22 2009 at 08:55 BST

    @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.

  9. Suhail
    Suhail
    Jul 7 2010 at 16:50 BST

    Thanks

    ${'s'.'t'.'r'} = 'Hello Peter';

    Helped a lot…..
    after hours of solution searching …
    & solving problem

  10. Joshua D. Burns
    Sep 10 2010 at 16:50 BST

    what about:

    <code lang="php">$foo = 'hi';
    $bar = 'ho';
    ${$foo . $bar} = 'Yes!'; // Same as $hiho</code>
  11. Jun 28 2011 at 14:25 BST

    [...] Cowburn.info [...]

  12. Bozo
    Bozo
    Jan 9 2012 at 18:34 BST

    This also works with object members, where I had to use this recently to deal with a database wrapper:
    $r->1st_column
    isn’t valid, but
    $r->{'1st_column'}
    works as expected.

  13. Bitcoin FAQ
    Bitcoin FAQ
    Feb 16 2012 at 03:40 BST

    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);

  14. Peter
    Feb 16 2012 at 13:26 BST

    Bitcoin FAQ, yes that will work as expected. :)

Post Comment