RE: [PHP] HTML lists & PHP
| From: | Ford, Mike [LSS] | Date: | Wed, 30 Jan 2002 12:14:24 +0000 |
| Subject: | RE: [PHP] HTML lists & PHP | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-82722@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Phil Schwarzmann [mailto:pschwar@jhmi.edu]
> Sent: 28 January 2002 22:02
>
> I keep getting parse error with this code:
> ($teams[] is a big array that I got by using mysql_fetch_array)
>
>
> $r = "game";
> echo "<select name='game'>";
> echo "<option value=game1>$teams[$r.'1']</option>";
> echo "<option value=game1>$teams[$r.'16']</option>";
> echo "</select>";
This really is a faq: if you want to substitute anything other than a simple variable name into a
double-quoted string, you MUST use curly braces {} to delimit it. So:
echo "<option value=game1>${teams[$r.'1']}</option>";
is what you need.
(Note: I also treat variable names including underlines _ as being not simple, as I've had
cases where PHP seems to parse these incorrectly -- for example, "...$long_name..." is
treated as "...${long}_name...", not "...${long_name}...".)
If you have any doubts, include the {} -- even for simple variables, it does no harm to write, for
example "...${var}...".
I do sometimes wonder whether the {} should be made mandatory in some future version, even for
simple variables, to avoid exactly this kind of confusion.
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211