5

I ran across this inside a WordPress plugin.

$stuff = $wpdb->get_results(" assume valid database query here ");
foreach ($stuff as $cur)
    ${$cur->type}[] = $cur->name;

$stuff will be an object containing more objects of database rows. These database rows will have columns 'id', 'type', and 'name'. The 'type' column will contain one of these three strings: 'file', 'url', or 'code'.

It looks like this code snippet will potentially create or add new elements to arrays named $file, $url, and/or $code. However, I'm not familiar with this use of the ${$ } syntax; I've only seen it inside double quoted strings to avoid parsing problems.

Am I correct in my analysis of this code? Where can I learn more about this use of the ${$ } syntax?

There is a question about the ${ } syntax inside a double-quoted string. I understand that use, but I'm specifically asking about a second $ character inside the { } braces.

Community
  • 1
  • 1
  • `{$cur->type}` forces this bit to resolve first, ensuring that `$$cur` isn't the meaning, nor `$cur->type[]`. If you want to know more about the overall approach, look up [variable variables](https://duckduckgo.com/?q=php+variable+variables) - it's a way of writing an expression in which the variable names themselves may change. – halfer Aug 21 '13 at 20:26
  • @j08691: That duplicate is actually about something different. Not totally different but not the case here. – hakre Aug 21 '13 at 20:26
  • 3
    http://www.php.net/manual/en/language.variables.variable.php – Fouad Fodail Aug 21 '13 at 20:27
  • @j08691, I changed the title slightly to look less like a duplicate of that question, which, in my opinion, it is not. – Ben Miller - Remember Monica Aug 21 '13 at 20:32
  • 1
    @Ben - if this question closes as a duplicate of the item j08691 has proposed, ping me and I'll vote to reopen. That mentions the `{}` but not the `$$`. – halfer Aug 21 '13 at 20:32
  • 1
    @halfer, it was marked as duplicate. I've got my answer, but if you want to reopen, feel free. It's not a duplicate of that question. If anything, it's a duplicate of [this one](http://stackoverflow.com/q/15548950/2600278). – Ben Miller - Remember Monica Aug 22 '13 at 03:31

3 Answers3

4

It's a "variable variable". In this case, it is referring to the array in the variable named whatever is in $cur->type.

Personally I hate this, and would much prefer to do this:

$someArray[$cur->type][] = $cur->name;
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 2
    No, variable variable is something else in PHP. That's the thing with `$$` at the beginning: http://php.net/manual/en/language.variables.variable.php – hakre Aug 21 '13 at 20:27
  • 1
    @hakre in this case it is variable variable, but the one holding final variable name is in complex form. – dev-null-dweller Aug 21 '13 at 20:36
  • @dev-null-dweller: I don't think so. Property access is more than a single variable that then is the second in variable variable. I'd say this is more like [String Parsing](http://www.php.net/language.types.string.php#language.types.string.parsing) then like [Variable Variables](http://php.net/language.variables.variable.php). – hakre Aug 21 '13 at 20:39
  • 1
    Meh, the example indeed is on the variable variable page, I stand corrected: *"Curly braces may also be used, to clearly delimit the property name. They are most useful when accessing values within a property that contains an array, when the property name is made of mulitple parts, or when the property name contains characters that are not otherwise valid (e.g. from json_decode() or SimpleXML)."* - at least both is correct, manual also on the string page I favored calls it *Complex (curly) syntax*. It's just perhaps there is no central place that documents it. – hakre Aug 21 '13 at 20:45
4

Consider

$foo = 42;
$a = 'foo';
echo $$a; // Prints 42

That´s called a variable variable, since the variable´s name is determinated at runtime. But is $$a[1] the same as ${$a[1]} or the same as {$$a}[1]? The brackets avoid that ambiguity, just like they do when dealing with operator precedence in math.

Zsolt Szilagyi
  • 4,741
  • 4
  • 28
  • 44
2

those curly brackets open a string context evaluating the expression there-in to a string which then is used as variable name.

For example, if you want to write a variable with a name you could never write in normal PHP:

echo $;

The variable with an empty name. PHP would give you a syntax error. However you can create such variable with the curly brackets:

${''} = 'hello';
echo ${''}; # prints "hello"

that is a variable with an empty name. This can be helpful if you want to evaluate the variable name first:

${$cur->type}[]
| ### 1. ###|
`---- 2. ---´

This allows you to clearly say where the array is. In the variable named by $cur->type.

I probably can not explain it really well, this is a blog post I remember so it's probably worth on the topic:

hakre
  • 193,403
  • 52
  • 435
  • 836