RE: [PHP] Building Dynamic Value list using ohp
| From: | Philip Olson | Date: | Wed, 26 Sep 2001 16:28:04 +0000 |
| Subject: | RE: [PHP] Building Dynamic Value list using ohp | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-68894@lists.php.net to get a copy of this message | ||
> The reason for it is that within double quote you can parse for 'simple'
> variables only (es: $var, not $var[]). Otherwise it gives you a parse
> error.
Just a quick note, this is partially true. The following works :
error_reporting(E_ALL);
$arr = array ('a' => 'apple','b' => 'banana');
print "I'd like an $arr[a] today, and a {$arr['b']} too";
I used to assume "foo $arr[a]" would give a Warning but it doesn't as the
constant 'a' isn't looked for in the string, but :
print $arr[a]; // apple and a Warning
// Warning: Use of undefined constant a - assumed 'a'
define('a','b'); // define a as a constant with value of b
print $arr[a]; // banana and no Warning
print $arr['a']; // apple and no Warning (quotes are good)
Numerical arrays work fine in a string, such as "foo $array[0]".
Maxim is referring to "foo $arr['a']" creating a parse error, one that
looks like :
Parse error: parse error, expecting
T_STRING' or T_VARIABLE' or
`T_NUM_STRING' in /www/htdocs/test.php on line 4
As it does. The Warnings shown above refer to the E_NOTICE level. Have
fun and yes, this is a digression and off-topic :)
Regards,
Philip Olson
> 1. this:
> print("<option value=\"$row[0]\">$row[0]</option>");
> should be this:
> print("<option value=\"{$row[0]}\">{$row[0]}</option>");
> or like this:
> echo '<option
> value="'.$row[0].'">'.$row[0].'</option>');
>
> The reason for it is that within double quote you can parse for 'simple'
> variables only (es: $var, not $var[]). Otherwise it gives you a parse
> error.