|
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
[Back to original message]
|