0

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.

0

7 Answers 7

2

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']);
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0
echo("<option value='" . $_GET['b'] . "'>" .$_GET['b'] . "</option>");

Comments

0
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.

Comments

0

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>';

Comments

0

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.

Comments

0
printf("<option value=\"%s\">%s</option>", $_GET['b'], $_GET['b']);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.