What is the purpose of using the <option selected> tag in PHP and how does it relate to comparing data with a database?

The <option selected> tag in PHP is used to pre-select an option in a dropdown menu. This can be useful when populating a dropdown menu with data from a database and wanting to show a specific value as selected based on a comparison with data from the database.

&lt;select name=&quot;dropdown&quot;&gt;
    &lt;?php
    $options = array(&quot;Option 1&quot;, &quot;Option 2&quot;, &quot;Option 3&quot;);
    $selectedOption = &quot;Option 2&quot;; // Data from the database

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