You are here: Re: Does PHP5 support dynamic variables? « PHP Language « IT news, forums, messages
Re: Does PHP5 support dynamic variables?

Posted by Steve on 09/28/07 14:18

"Erwin Moller"
<Since_humans_read_this_I_am_spammed_too_much@spamyourself.com> wrote in
message news:46fcc791$0$244$e4fe514c@news.xs4all.nl...
> Old Caledonia wrote:
>> Hi,
>>
>> I'm in the process of upgrading an aged php application to php5
>> compatibility.
>>
>> One script has me stumped. Here's a code snippet:
>>
>> while($categoryrow = mysql_fetch_array($categoryresult)) {
>> $categoryval = $categoryrow["category"];
>> $categoryidval = $categoryrow["categoryid"];
>> $tempbox = "box" . $categoryidval;
>>
>> //assign dynamic variable
>> $categoryvalue = ${$tempbox};
>>
>> // end code snippet.
>>
>> If I echo $tempbox, I get a list of numerical values (correctly).
>> If I echo $categoryvalue, my data is empty.
>>
>> If it's not a PHP5 thing then I'm lost - any ideas/suggestions?
>>
>> Neil
>>
>>
>
> Hi Neil,
>
> In addition to what others wrote already:
> I totally prefer to avoid this approach.
> In my humble opinion it is much cleaner to create an associative array
> that holds the 'variablename' as a key.
>
> So Instead of creating:
> $box23
> $boxHenry
> $boxBilletheFirst
> $box45
> etc
>
> I rather see:
> $myStuff = array();
> $myStuff["box23"] = whateverGoesInHere;
> $myStuff["boxHenry"] = whateverGoesInHere;
> $myStuff["boxBilletheFirst"] = whateverGoesInHere;
>
>
> In this way you create a seperate 'namespace' by using your variables in
> the array as keys. (Not really a real namespace, but it comes close in
> this context).
> I think that is much cleaner code.

BINGO!!!

$$variable is a bad construct and usually points to even worse design flaws
in a system. i've been programming since php 3. i still have yet to find it
needed or useful.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация