6

I was showing someone how you can create variable variable variables in PHP (I'd only recommend using them NEVER, it's horrible practice and you are a bad person if you use variable variable variables in actual production code), and they asked if the dollar sign acted as a dereference operator in this case.

It doesn't actually create a reference to the other variables, so I don't really see it as being the deref op. The documentation for variable variables doesn't even mention references at all.

Who's right? I don't think variable variables are creating references and therefore the dollar sign isn't the dereference operator.

Here's some sample code for your viewing pleasure (or pain given the contents):

<?php

$a = 'c';
$b = 'a';
$c = 'hello';

echo($$$b); //hello
Cyclone
  • 17,939
  • 45
  • 124
  • 193
  • 1
    The documentation doesn't refer to them as such. And the semantics of variable variables are clear. So why does it matter? – Oliver Charlesworth Sep 06 '11 at 21:18
  • 3
    If you mean dereference as in the C-style `*x` type thing, then it's kinda-sort-maybe-not-really the same thing. A C dereference is "take the number stored in this variable and treat it as a memory address". This "dereference" is PHP is "take the string stored in this variable and use it at as a variable name". – Marc B Sep 06 '11 at 21:19
  • @personwhovotedtoclose: This is definitely a real question, about how the language works. – Cyclone Sep 06 '11 at 21:19
  • 1
    @Cyclone: It's not really a question about how the language works, because it's quite clear you already know how what that code example does! You're really just asking "is it valid to call this language construct by this other name?" – Oliver Charlesworth Sep 06 '11 at 21:28
  • 1
    There is no such thing as a *dereference operator* in PHP; neither among the [list of operators](http://php.net/manual/en/language.operators.php), nor in the [list of parser tokens](http://php.net/manual/en/tokens.php). If a `$` is encountered, the parser will greedily take as many tokens as possible to form a valid variable name and that's it. – Gordon Sep 06 '11 at 21:43

2 Answers2

4

Is the dollar sign in a variable variable considered the dereference operator?

No. PHP does not possess a dereference operator.

Variable variables shouldn't be thought of as dereferencing but, rather, accessing the symbol tree via a string. For example:

$bar = 1;
echo ${'bar'};

You can perform this dynamically by using a variable instead of a string literal:

$bar = 1;
$foo = 'bar';
echo ${$foo};

PHP syntax allows you to remove the braces but it's still a matter of accessing the symbol table via a string. No referencing/dereferencing involved.

webbiedave
  • 48,414
  • 8
  • 88
  • 101
0

No, it is not DE-referencing anything....if anything at all, it is referencing the reference of a stored variable name to reference the stored variable name's stored value....kind of a double reference or reference of the reference.....de-ref would mean that one variable was part of the subset of another.