14

I saw this code

if (is_null($$textVarName)) {
$$textVarName = $_defaultTexts[$type];
}

what is code "$$" ?

ajreal
  • 44,929
  • 10
  • 81
  • 118
meotimdihia
  • 4,417
  • 14
  • 45
  • 65
  • possible duplicate of [what does $$ mean in PHP?](http://stackoverflow.com/questions/2715654/what-does-mean-in-php) – Gordon Nov 13 '10 at 09:12
  • *(related)* [What does that symbol mean in PHP](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Gordon Nov 13 '10 at 09:12
  • Possible duplicate of [What does $$ (dollar dollar or double dollar) mean in PHP?](https://stackoverflow.com/questions/2715654/what-does-dollar-dollar-or-double-dollar-mean-in-php) – Shivam Sharma Dec 17 '18 at 14:29

3 Answers3

38

It's evil is what it is.

That will take the value that's in $textVarName and use that as a variable name. For example:

$foo = 'hello';
$hello = 'The Output';
echo $$foo; // displays "The Output"
VoteyDisciple
  • 35,315
  • 5
  • 85
  • 89
  • @VoteyDisciple; is that endless? Can I have `$$$foo`? Or it stops at two? – BeemerGuy Nov 12 '10 at 23:22
  • 6
    @Beemer: it is endless. Btw, it is faster to try - than ask and wait for answer. – zerkms Nov 12 '10 at 23:23
  • @zerkms: thanks for the answer, but did it occur to you that I might not have a compiler handy? – BeemerGuy Nov 12 '10 at 23:24
  • @zerkms: Cool link, which is exactly why I asked my question. I wouldn't have known about this link if I didn't ask that question. I ask to get more than what I just asked. Thanks! – BeemerGuy Nov 12 '10 at 23:33
  • 1
    i can see how it would be confusing and thus "evil", but this is so elegant. It's quite a useful feature at times, which requires messy syntax in other languages to accomplish. It's not too different from dereferencing pointers in C++. If that's fine, then so should this. If this is evil, then so would multiple pointer dereferences in C++ – ahnbizcad May 09 '15 at 07:09
  • Multiple pointer dereferences in C++ are pretty evil too (hence all the post-C++ languages that no longer include explicit declared pointers), but at least they're expected. What makes this particularly wicked in PHP is that it's exceptionally unusual, and any quick reading of the source is likely to overlook a rogue `$$` in there. I have never encountered a situation where a variable variable was a better choice than a hash map or other data structure. – VoteyDisciple May 12 '15 at 16:06
4
foreach($_POST as $key=>$value)$$key=$value;

now, automagically, if the previous form had a field named 'username' you now have a variable called $username that holds the value submitted in the form. not the greatest or secure method, but when you have a pocket full of nails, this is a heck of a hammer

this is pretty bad practice and is never encouraged but all PHP coders I know secretly sorta like it.

FatherStorm
  • 7,015
  • 1
  • 18
  • 25
  • 1
    Hi, I'm cfreak and I'm a PHP coder. Now you know me. I absolutely hate it. :-) (of course I don't really like PHP at all but it pays the bills so I do it) – Cfreak Nov 12 '10 at 23:25
  • Despite it being bad practice... I love it!! I once had like 50 entries in the `$_POST` and it was definitely not fun to make a variable for each (even with automating that with something like Excel). Wish I knew this shortcut back then. – BeemerGuy Nov 12 '10 at 23:28
  • 5
    Note that you can use `extract($_POST,EXTR_PREFIX_ALL,"p_")` to achieve the same effect - plus more security with a variable prefix. Bonus points for `array_map` filtering. – mario Nov 12 '10 at 23:37
  • Plus, what's wrong with just writing `$_POST['foo']`? I've ***never*** felt compelled to extract (manually or otherwise) a variable from `$_POST` unless I had a specific reason to do additional processing with that one variable. – VoteyDisciple Nov 13 '10 at 00:27
2

For reference, see: http://php.net/manual/en/language.variables.variable.php

Prisoner
  • 25,993
  • 10
  • 68
  • 97