RE: [PHP] Re: Forms in PHP
| From: | Andrew Chase | Date: | Fri, 12 Apr 2002 16:36:42 +0000 |
| Subject: | RE: [PHP] Re: Forms in PHP | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-92789@lists.php.net to get a copy of this message | ||
>May I also suggest that you rewrite your echo statements as:
>
> echo '<OPTION VALUE="donate">Donate this item</OPTION>';
>
>Vastly improves legibility.
It's a problem, however, if you want to include a PHP variable in that echo
statement, if you're populating a SELECT menu with a loop, for instance;
$menu_options = array("A" => "Option 1", "B" => "Option
2", "C" => "Option
3");
foreach(array_keys($menu_options) as $option_value){
$option_text = $menu_options[$option_value];
echo '<OPTION VALUE="$option_value">$option_text</OPTION>';
}
would just output
<OPTION VALUE="$option_value">$option_text</OPTION>
<OPTION VALUE="$option_value">$option_text</OPTION>
<OPTION VALUE="$option_value">$option_text</OPTION>
which is probably not the desired output :) You need to use double quotes
for variable substitution. Unfortunately, if you want to maintain XHTML
compliance the tag attributes have to be double-quoted too...
echo "<OPTION VALUE=\"$option_value\">$option_text</OPTION>\n";
(Might as well add a "\n" at the end of the line for readability in the
browser's 'View Source' window!)