What is $$?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

What is $$?

Post by smudge »

Hi, I just read this post: viewtopic.php?t=70504 and came across this:

Code: Select all

while (list ($key,$val) = each ($_POST)) {
    $$key = $val;
}
What exactly is the $$? I understand what list and each do, but I have never seen $$ before. Is it a typo or does it have a special meaning?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

Thanks much! You learn something new every day!
User avatar
ReverendDexter
Forum Contributor
Posts: 193
Joined: Tue May 29, 2007 1:26 pm
Location: Chico, CA

Post by ReverendDexter »

Something you don't ever want to use if you can avoid it ;)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yup, they're totally lame. If you're considering using variable variables, you likely need an array instead.

Let me qualify that: they're totally lame for most purposes -- they do come in handy in some very rare cases.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

And if you have to use them, help out the maintainer of the code later by:
  1. Commenting what you are doing
  2. Commenting why you are doing it
  3. Using curly braces around the variable you are variablizing:

    Code: Select all

    <?php
    foreach($_POST as $k => $v) {
      ${$k} = $v;
    }
    ?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

ReverendDexter wrote:Something you don't ever want to use if you can avoid it ;)
Although rare, there are times when they come in very, very handy.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Everah wrote:Using curly braces around the variable you are variablizing
Keep in mind that the placement of braces can change the meaning. IE Whether your using an array index or character offset.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

astions wrote:
Everah wrote:Using curly braces around the variable you are variablizing
Keep in mind that the placement of braces can change the meaning. IE Whether your using an array index or character offset.
Yep but I agree with Everah's advice to use them otherwise it looks like it could have been done by accident. Personally I find $obj->${$prop} infinitely more useful than ${$var}
Post Reply