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

Posted by Erwin Moller on 09/28/07 09:18

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.

just my 2 cent..

Regards,
Erwin Moller

 

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

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