Sagewire Logo

Does PHP5 support dynamic variables?

8 Message(s) by 5 Author(s) originally posted in php language


From: Old Caledonia Date:   Thursday, September 27, 2007
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


From: Steve Date:   Thursday, September 27, 2007
Hi,
I'm in the process of upgrading an aged php application to php5
compatibility.



$categoryvalue = ${$tempbox};



listen, this construct isn't 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 line s of code to imperically answer your
own question.


From: Old Caledonia Date:   Thursday, September 27, 2007
Hi,

I'm in the process of upgrading an aged php application to php5
compatibility.
$categoryvalue = ${$tempbox};
listen, this construct isn't called or known by 'dynamic
variables'...which'd 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'd be dynamic - I understand - I did not 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've
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 real ly need some help with was understanding why $categoryvalue'd
be empty.


From: Steve Date:   Thursday, September 27, 2007
Hi,
I'm in the process of upgrading an aged php application to php5
compatibility.

$categoryvalue = ${$tempbox};

listen, this construct isn't called or known by 'dynamic
variables'...which'd 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'd be dynamic - I understand - I did not 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've
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 does not show where/how $tempbox
is being set.

just for fun, is the data entry form in the genre of a grid? ;^)


From: peter Date:   Thursday, September 27, 2007
What I really need some help with was understanding why $categoryvalue
would
be empty.



Try echoing out $tempbox and see if it looks as expect ed. Ensure that a
variable exists with the name of what it choes out. Another thing you could
do is ensure you've all error s being reported (error_reporting(E_ALL);).
If $tempbox isn't outputting what is expected then I'd 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 hasn't been defined yet I am trying to use
it.


From: Aaron Saray Date:   Friday, September 28, 2007
wrote in message:
> What I really need some help with was understanding why $categoryvalue
>'d
> 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've all errors being reported (error_reporting(E_ALL);).
If $tempbox isn't outputting what is expected then I'd 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 hasn't 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?


From: Erwin Moller Date:   Friday, September 28, 2007
wrote in message:
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,

wrote in message 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


From: Steve Date:   Friday, September 28, 2007
"Erwin Moller"
wrote in message in
message
wrote in message:
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,
wrote in message 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 have been programming since php 3. I still have yet to find it
needed or useful.



Next Message: query issue


Blogs related to Does PHP5 support dynamic variables?

Good morning Im running gentoomod_php4 and php5-cgi to aid in ...
ello ello. im wondering if some people in here could tell me their favourite php editors? does anything have decent ftp/scp support? code completion or code hinting? syntax highlighting too. currently using adobe dreamweaver as I work ...

i have 3 php files onephp twophp threephp onephp form posts a ...
there is one, foutrelis. php-bot? yers. Oh, if it does logging too then ok. I've no idea what that little bot does to be honest lol. hi, is there a page somewhere that lists the current state of php6 and the differences to php5 ? ...

I was learning Drupal and saw it had a db_query function that ...
[dev]Viper: what server does that? is installed. apache, all versions. no, Install libmysqlclient-whatever-dev. PHP 4 and PHP 5 I don't know if I found a bug or this is supposed to work like that. [dev]Viper: never saw it do anything ...

Very tricky php question I have an object with several public ...
we tell them PHP 5 is the minimum requirement actually we tell them PHP 5.2 we have a list of hosts that have it or we can offer to host it for them for a fee. does php have lists and maps like in java ? php has an array ...

hi ive installed a server apache on my linux distro added php but ...
But to each his own. does that make sense? 12) { echo "good morning";}? has anyone installed php5 on their mac? I am about to do the install at http://www.entropy.ch/software/macosx/php/ but I am afraid it will break system updates ...

hi i have a little problem i export a web page html to a word ...
Linux maybe? Do you know what distribution? dump? what is it like? var_dump($_SESSION). moment. what does it do? Unix PHP 5, Apache 2. var_dump dumps out all of the values in a variable for a hash it will dump out everything in the hash ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional