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
smudge
Forum Contributor
Posts: 151 Joined: Sun May 20, 2007 12:13 pm
Post
by smudge » Thu Jul 12, 2007 10:59 am
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?
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Thu Jul 12, 2007 11:05 am
smudge
Forum Contributor
Posts: 151 Joined: Sun May 20, 2007 12:13 pm
Post
by smudge » Thu Jul 12, 2007 11:07 am
Thanks much! You learn something new every day!
ReverendDexter
Forum Contributor
Posts: 193 Joined: Tue May 29, 2007 1:26 pm
Location: Chico, CA
Post
by ReverendDexter » Thu Jul 12, 2007 11:56 am
Something you don't ever want to use if you can avoid it
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Thu Jul 12, 2007 11:58 am
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.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Thu Jul 12, 2007 1:01 pm
And if you have to use them, help out the maintainer of the code later by:
Commenting what you are doing
Commenting why you are doing it
Using curly braces around the variable you are variablizing:
Code: Select all
<?php
foreach($_POST as $k => $v) {
${$k} = $v;
}
?>
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Thu Jul 12, 2007 1:57 pm
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.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Thu Jul 12, 2007 1:59 pm
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.
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Thu Jul 12, 2007 2:06 pm
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}