Re: Variable variables and references

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
To: PHP-General <php-general@xxxxxxxxxxxxx>
Sent: Saturday, March 10, 2007 5:28:57 AM
Subject:  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




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux