Re: Variable variable
| From: | Steve Werby | Date: | Wed, 18 Apr 2001 13:48:36 +0000 |
| Subject: | Re: Variable variable | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-49211@lists.php.net to get a copy of this message | ||
"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/