What is the purpose of the ternary operator in PHP code and how is it used effectively in <select> elements?
The ternary operator in PHP is used as a shorthand way to write if-else statements. It allows for a more concise and readable code. When used effectively in <select> elements, the ternary operator can dynamically set the selected attribute for specific options based on certain conditions.
<select>
<option value="1" <?php echo ($selectedOption == 1) ? 'selected' : ''; ?>>Option 1</option>
<option value="2" <?php echo ($selectedOption == 2) ? 'selected' : ''; ?>>Option 2</option>
<option value="3" <?php echo ($selectedOption == 3) ? 'selected' : ''; ?>>Option 3</option>
</select>