How can the selected option be highlighted in a multiple selection menu generated dynamically in PHP?

To highlight the selected option in a multiple selection menu generated dynamically in PHP, you can use the "selected" attribute in the HTML <option> tag. You need to check if the current option being generated matches the selected option and add the "selected" attribute accordingly.

&lt;?php
// Sample array of options
$options = [&#039;Option 1&#039;, &#039;Option 2&#039;, &#039;Option 3&#039;];

// Selected option
$selectedOption = &#039;Option 2&#039;;

// Generate the select menu
echo &#039;&lt;select name=&quot;selectMenu&quot; multiple&gt;&#039;;
foreach ($options as $option) {
    if ($option == $selectedOption) {
        echo &#039;&lt;option value=&quot;&#039; . $option . &#039;&quot; selected&gt;&#039; . $option . &#039;&lt;/option&gt;&#039;;
    } else {
        echo &#039;&lt;option value=&quot;&#039; . $option . &#039;&quot;&gt;&#039; . $option . &#039;&lt;/option&gt;&#039;;
    }
}
echo &#039;&lt;/select&gt;&#039;;
?&gt;