How can the selected attribute be used in PHP to highlight a specific value within a dropdown menu generated by a function?

To highlight a specific value within a dropdown menu generated by a function in PHP, you can use the "selected" attribute in the HTML <option> tag. By setting the "selected" attribute to "selected" for the desired option, you can pre-select that option in the dropdown menu. This can be achieved dynamically by checking the selected value against the current option being generated by the function and adding the "selected" attribute accordingly.

&lt;select name=&quot;dropdown&quot;&gt;
    &lt;?php
    $options = array(&#039;Option 1&#039;, &#039;Option 2&#039;, &#039;Option 3&#039;);
    $selectedValue = &#039;Option 2&#039;; // Value to be highlighted

    foreach ($options as $option) {
        if ($option == $selectedValue) {
            echo &quot;&lt;option value=&#039;$option&#039; selected&gt;$option&lt;/option&gt;&quot;;
        } else {
            echo &quot;&lt;option value=&#039;$option&#039;&gt;$option&lt;/option&gt;&quot;;
        }
    }
    ?&gt;
&lt;/select&gt;