Re: Variable variable

From: Date: Wed, 18 Apr 2001 15:01:07 +0000
Subject: Re: Variable variable
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-49228@lists.php.net to get a copy of this message
> The braces aren't required in this case. Yup, your right. I stand corrected. I had forgotten that multiple $s are fine, and that I simply don't use them because I have trouble reading them. The joys of learning ;\ -- Plutarck Should be working on something... ...but forgot what it was. ""Steve Werby"" <steve-lists@befriend.com> wrote in message news:001e01c0c80e$45410400$6501a8c0@workstation7... > "Plutarck" <aemuli@spamclause-yahoo.com> wrote: > > Your problem has little to do with the things mentioned. > > > > First of all, you are refering to a variable variable incorrectly. > > Actually, he's not. > > >You must > > use brackets, like this: > > > > ${$id} > > The braces aren't required in this case. Of the four examples below, only > the 3rd won't return the expected results - because the first $ is > interpreted as a literal $. Paste the code below into a webpage and see for > yourself. > > <?php > $field = 'name'; > $name = 'Steve'; > > echo '<br>' . $$field; > echo '<br>' . ${$field}; > echo '<br>' . "My name is $$field."; > echo '<br>' . "My name is ${$field}."; > ?> > > I have a few possible explanations for the problem of the original poster > (see below). > > > if (${$id} == "on") > > { > > echo 'on'; > > } else > > { > > echo 'off'; > > } > > 1. If $id is a number I believe PHP will choke b/c I don't believe numbers > can be used as variable names. > > 2. Assume $id = 'age_bracket'. The control structure checks to see if > $age_bracket == "on". To set $id in the first place for your multiple > checkbox values, you'd have to loop through the values sent from the form. > Jack (original poster), did you do that? If your form's method = post then > you would loop through $HTTP_POST_VARS and if it was get $HTTP_GET_VARS (and > then only loop through the checkbox variables). Or you could predefine the > checkbox variables using: > > $checkbox_fields = array( 'age_bracket', 'weight', 'gender', 'occupation' ); > > And then loop through as follows: > > while( list( , $val ) = each( $checkbox_fields ) ) > { > // may have to set as $$GLOBALS[$val] if within a function or declare $val > as global first. > if ( $$val == 'on' ) > { > echo 'on'; > } > else > { > echo 'off'; > } > } > > -- > Steve Werby > President, Befriend Internet Services LLC > http://www.befriend.com/ > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#49228) next »