PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.php > Does PHP5 support dynamic variables?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Does PHP5 support dynamic variables?

Réponse
 
LinkBack Outils de la discussion
Vieux 27/09/2007, 21h34   #1
Old Caledonia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Does PHP5 support dynamic variables?

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


  Réponse avec citation
Vieux 27/09/2007, 21h55   #2
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Does PHP5 support dynamic variables?


"Old Caledonia" <oldcaledonia@tiscali.co.uk> 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 yourself out with 3 lines of code to imperically answer your
own question.


  Réponse avec citation
Vieux 27/09/2007, 22h19   #3
Old Caledonia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Does PHP5 support dynamic variables?


"Steve" <no.one@example.com> wrote in message
news:tMUKi.92$Ga7.49@newsfe06.lga...
>
> "Old Caledonia" <oldcaledonia@tiscali.co.uk> 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 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 with was understanding why $categoryvalue would
be empty.




  Réponse avec citation
Vieux 27/09/2007, 22h22   #4
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Does PHP5 support dynamic variables?


"Old Caledonia" <oldcaledonia@tiscali.co.uk> wrote in message
news:GfadnSHXkKfLg2HbnZ2dnUVZ8sOonZ2d@bt.com...
>
> "Steve" <no.one@example.com> wrote in message
> news:tMUKi.92$Ga7.49@newsfe06.lga...
>>
>> "Old Caledonia" <oldcaledonia@tiscali.co.uk> 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 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 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? ;^)


  Réponse avec citation
Vieux 28/09/2007, 01h35   #5
peter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Does PHP5 support dynamic variables?


> What I really need some 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.


  Réponse avec citation
Vieux 28/09/2007, 05h15   #6
Aaron Saray
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Does PHP5 support dynamic variables?

On Sep 27, 7:35 pm, "peter" <sub...@flexiwebhost.com> wrote:
> > What I really need some 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?

  Réponse avec citation
Vieux 28/09/2007, 10h18   #7
Erwin Moller
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Does PHP5 support dynamic variables?

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
  Réponse avec citation
Vieux 28/09/2007, 15h18   #8
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Does PHP5 support dynamic variables?


"Erwin Moller"
<Since_humans_read_this_I_am_spammed_too_much@spam yourself.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.


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h53.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15703 seconds with 16 queries