Does PHP5 support dynamic variables?
Does PHP5 support dynamic variables?
am 27.09.2007 22:34:15 von Old Caledonia
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
Re: Does PHP5 support dynamic variables?
am 27.09.2007 22:55:08 von Steve
"Old Caledonia" wrote in message
news:deGdndSVNucyjmHbnZ2dnUVZ8s6gnZ2d@bt.com...
> Hi,
>
> I'm in the process of upgrading an aged php application to php5
> compatibility.
> $categoryvalue = ${$tempbox};
listen, this construct is not called or known by 'dynamic variables'...which
would be a moot point. [hint: 'variable'] ;^)
anyway, yes, the $$variable and ${$variable} *variable variable* (not just
symantics) construct is very much alive in php 5 - most often used by those
who have programmed themselves into a corner. ;^)
you could help yourself out with 3 lines of code to imperically answer your
own question.
Re: Does PHP5 support dynamic variables?
am 27.09.2007 23:19:42 von Old Caledonia
"Steve" wrote in message
news:tMUKi.92$Ga7.49@newsfe06.lga...
>
> "Old Caledonia" wrote in message
> news:deGdndSVNucyjmHbnZ2dnUVZ8s6gnZ2d@bt.com...
>> Hi,
>>
>> I'm in the process of upgrading an aged php application to php5
>> compatibility.
>
>> $categoryvalue = ${$tempbox};
>
> listen, this construct is not called or known by 'dynamic
> variables'...which would be a moot point. [hint: 'variable'] ;^)
>
> anyway, yes, the $$variable and ${$variable} *variable variable* (not just
> symantics) construct is very much alive in php 5 - most often used by
> those who have programmed themselves into a corner. ;^)
>
> you could help yourself out with 3 lines of code to imperically answer
> your own question.
Okay so a variable would be dynamic - I understand - I didn't write the
code, I just copied and pasted it. The 'dynamic variable' comment was there
from before. And I accept that to answer the question I could have
constructed another procedure to test if PHP5 did/did_not support the
construct. So I guess I asked the wrong question then - sorry about that
:¬)
What I really need some help with was understanding why $categoryvalue would
be empty.
Re: Does PHP5 support dynamic variables?
am 27.09.2007 23:22:55 von Steve
"Old Caledonia" wrote in message
news:GfadnSHXkKfLg2HbnZ2dnUVZ8sOonZ2d@bt.com...
>
> "Steve" wrote in message
> news:tMUKi.92$Ga7.49@newsfe06.lga...
>>
>> "Old Caledonia" wrote in message
>> news:deGdndSVNucyjmHbnZ2dnUVZ8s6gnZ2d@bt.com...
>>> Hi,
>>>
>>> I'm in the process of upgrading an aged php application to php5
>>> compatibility.
>>
>>> $categoryvalue = ${$tempbox};
>>
>> listen, this construct is not called or known by 'dynamic
>> variables'...which would be a moot point. [hint: 'variable'] ;^)
>>
>> anyway, yes, the $$variable and ${$variable} *variable variable* (not
>> just symantics) construct is very much alive in php 5 - most often used
>> by those who have programmed themselves into a corner. ;^)
>>
>> you could help yourself out with 3 lines of code to imperically answer
>> your own question.
>
> Okay so a variable would be dynamic - I understand - I didn't write the
> code, I just copied and pasted it. The 'dynamic variable' comment was
> there from before. And I accept that to answer the question I could have
> constructed another procedure to test if PHP5 did/did_not support the
> construct. So I guess I asked the wrong question then - sorry about that
> :¬)
>
> What I really need some help with was understanding why $categoryvalue
> would be empty.
that's a whole other ball of wax! your code doesn't show where/how $tempbox
is being set.
just for fun, is the data entry form in the genre of a grid? ;^)
Re: Does PHP5 support dynamic variables?
am 28.09.2007 02:35:04 von Peter
> What I really need some help with was understanding why $categoryvalue
> would
> be empty.
Try echoing out $tempbox and see if it looks as expected. Ensure that a
variable exists with the name of what it choes out. Another thing you could
do is ensure you have all errors being reported (error_reporting(E_ALL);).
If $tempbox is not outputting what is expected then I would expect an error
message stating an undefined variable such as:-
Notice: Undefined variable: b in php-file on line 4
This indicates that variable b has not been defined yet I am trying to use
it.
Re: Does PHP5 support dynamic variables?
am 28.09.2007 06:15:22 von Aaron Saray
On Sep 27, 7:35 pm, "peter" wrote:
> > What I really need some help with was understanding why $categoryvalue
> > would
> > be empty.
>
> Try echoing out $tempbox and see if it looks as expected. Ensure that a
> variable exists with the name of what it choes out. Another thing you could
> do is ensure you have all errors being reported (error_reporting(E_ALL);).
> If $tempbox is not outputting what is expected then I would expect an error
> message stating an undefined variable such as:-
>
> Notice: Undefined variable: b in php-file on line 4
>
> This indicates that variable b has not been defined yet I am trying to use
> it.
Lets make the assumption that your categoryid is a numeric value...
and its going 1, 2, 3, etc... for all of your values and the
categories corresponding are dog, cat, fish
categoryid, category
1, dog
2, cat
3, fish
So the first time through, you're going to have this::
//1st iteration:
//categoryval lets say this is 'dogs'
$categoryval = $categoryrow["category"];
//categoryidval = 1
$categoryidval = $categoryrow["categoryid"];
//tempbox = 'box1'
$tempbox = "box" . $categoryidval;
//$tempbox = 'box1'... variable of that means we're looking for a
variable named $box1 now.
$categoryvalue = ${$tempbox};
Was $box1 defined? No? could be blank categoryvalue then.
As suggested above, turn up your error_reporting... also maybe paste
in any other relevant details - ie, where is $box1 being defined?
Re: Does PHP5 support dynamic variables?
am 28.09.2007 11:18:33 von Erwin Moller
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
Re: Does PHP5 support dynamic variables?
am 28.09.2007 16:18:36 von Steve
"Erwin Moller"
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.