Re: Variable variables and references
| From: | Matt Carlson | Date: | Sat, 10 Mar 2007 16:01:34 +0000 |
| Subject: | Re: Variable variables and references | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-250204@lists.php.net to get a copy of this message | ||
I've used variable variables probably 5 times in 2 years. They are great when you need them,
but don't usually have a day-to-day use for them.
Here is some sample code of the last time I used it.
if(isset($$key))
{
print($$key);
continue;
} else {
$iquery = "SELECT * FROM
".ROSTER_ITEMSTABLE." WHERE
item_name = '".$iname."' AND member_id =
'".$row['member_id']."'";
$iresult = $wowdb->query($iquery);
$idata = $wowdb->fetch_assoc($iresult);
$item = new item($idata);
$$key = $item->out();
print $$key;
}
Basically, this is in a for-each loop, that steps through a list of keys for certain dungeons in
World of Warcraft. Instead of putting the data into an array, I used variable variables to stick
the data into a single variable. The way it works is $key = 'DM'; $$key = $data; The
literal translation for $$key is $DM once the code executes.
Hope this helps.
----- Original Message ----
From: Dave Goodchild <buddhamagnet@gmail.com>
To: PHP-General <php-general@lists.php.net>
Sent: Saturday, March 10, 2007 5:28:57 AM
Subject: [PHP] Variable variables and references
Hi guys, I have just read 'Programming PHP' (O'Reilly) and although I think
it's a great book, I am confused about variable variables and references -
not the mechanics, just where you would use them.
The subject of variable variables is explained but no examples are given as
to why and where you would utilise them.
As for references, the examples given with regard to passing and returning
by reference in functions is clear, but no real examples are given as to
when this would be a preferred approcah - in fact, the authors stress that
due to PHP's copy-on-write mechanism, it is not a frequently-used approcah.
So my question - are there any 'classic' situations in which either should
be used, and where and when do you guys use them in your real-world efforts?
--
http://www.web-buddha.co.uk