user2646006
user2646006

Reputation: 37

Why does this simple PHP code not work, yet the HTML does?

The first section of code (PHP) will only work if I comment out the 3 lines. The second section of code (HTML) works fine. I've tried many different iterations of the syntax of the 3 lines, but cannot get it to work.

<?php
echo '<select id="question" name="question">';
echo '<option value="Don't care" >Don't care</option>';
echo '<option value="Yes" selected="selected" >Yes</option>';
echo '<option value="No" >No</option>';
echo '</select>';
?>

<select id="question" name="question">
<option value="Don't care" >Don't care</option>
<option value="Yes" selected="selected" >Yes</option>
<option value="No" >No</option>
</select>

Thanks in advance!

Upvotes: 1

Views: 63

Answers (2)

hizbul25
hizbul25

Reputation: 3849

Take a close look in here:

echo '<option value="Don't care" >Don't care</option>';

Make a little change in your code, hope it will work.

echo'<option value="Don\'t care" >Don\'t care</option>';

Upvotes: 2

user229044
user229044

Reputation: 239581

You have a syntax error:

'<option value="Don't care"

The ' in Don't prematurely terminates your string. You need to escape it:

echo '<option value="Don\'t care" >Don\'t care</option>';

Your editors syntax highlighting should make this very obvious, as Stack Overflow demonstrates. You can clearly see in your question that "t care" is strangely higlighted, a dead giveaway that your string isn't terminating when you think it is.

Upvotes: 10

Related Questions