Reputation: 1889
I'm trying to write html code in a php echo statement but I keep failing.
echo "<option value='$_GET['b']'>$_GET['b']</option>";
error:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in .../web_info.php on line 11
I mean when should I use " and when '? what are the rules?that's so confusing.
Upvotes: 0
Views: 70
Reputation: 447
printf("<option value=\"%s\">%s</option>", $_GET['b'], $_GET['b']);
Upvotes: 0
Reputation: 146191
Simply you can write this (DEMO)
echo "<option value='$_GET[b]'>$_GET[b]</option>";
Or this (DEMO)
echo "<option value='" . $_GET['b'] . "'>" . $_GET['b'] . "</option>";
Read more on manual.
Upvotes: 0
Reputation: 5151
In addition to the other answers, a less well-known method is to use commas (so PHP doesn't have to do any concatenation):
echo '<option value="', $_GET['b'], '">', $_GET['b'], '</option>';
Upvotes: 0
Reputation: 3892
echo "<option value='".$_GET['b']."'>".$_GET['b']."</option>";
The problem is the single quote in the value attribute, you used to times and the parser cannot understand where is the end.
Upvotes: 0
Reputation: 76666
Wrap the variables in curly braces:
echo "<option value='{$_GET['b']}'>{$_GET['b']}</option>";
Or, use sprintf()
:
echo sprintf("<option value='%s'>%s</option>", $_GET['b'], $_GET['b']);
Upvotes: 2
Reputation: 5911
try this:
echo "<option value='{$_GET['b']}'>{$_GET['b']}</option>";
The problem was with the quotes and the brackets. Sometimes you need to wrap your variables in curly braces otherwise the PHP parser doesn't know when to start and stop for a variable. It gets mixed up with the rest of the string.
Upvotes: 2