Re: Variable variables and references

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

 



I'd agree with Tijnema! here, variable variables aren't really of any use here. However, a classic snippet of code is where one emulates register_globals functionality in a non-register_globals environment (not a good idea to do this btw, unless you know what you're doing and don't have any other way around it):
foreach($_POST as $key=>$val) {
	$$key = $val;
}
thus creating all
$image,$whatever,$and,$some,$more variables magically for you.

- tul

Tijnema ! wrote:
On 3/10/07, Matt Carlson <saryon@xxxxxxxxxx> wrote:

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.


In this case i should use an array, i think it's easier to use an array...
Array in an array in an array :)

But ofcourse sometimes you might (think you) need it.

Tijnema

----- 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




--
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