Skip to content
PHP Logo

What is the difference between using "selected" and "selected="selected"" in HTML select options in PHP?

When creating HTML select options in PHP, the difference between using "selected" and "selected='selected'" lies in the syntax. In HTML, the "selected" attribute is a boolean attribute that does not require a value, while in XHTML, it is recommended to use "selected='selected'" for compatibility. Therefore, to ensure compatibility across different browsers and standards, it is best to use "selected='selected'" when setting the selected option in a dropdown menu in PHP.

php
<select name="example">
    <option value="1" <?php echo ($selected == 1) ? 'selected="selected"' : ''; ?>>Option 1</option>
    <option value="2" <?php echo ($selected == 2) ? 'selected="selected"' : ''; ?>>Option 2</option>
    <option value="3" <?php echo ($selected == 3) ? 'selected="selected"' : ''; ?>>Option 3</option>
</select>