How can you add separators between groups of data in a dropdown list in PHP?

When creating a dropdown list in PHP, you may want to add separators between groups of data to make it more organized and easier to navigate. One way to achieve this is by using the <optgroup> tag in HTML, which groups related options together. By dynamically generating <optgroup> tags based on your data, you can create distinct sections within the dropdown list.

&lt;select&gt;
    &lt;optgroup label=&quot;Group 1&quot;&gt;
        &lt;option value=&quot;1&quot;&gt;Option 1&lt;/option&gt;
        &lt;option value=&quot;2&quot;&gt;Option 2&lt;/option&gt;
        &lt;option value=&quot;3&quot;&gt;Option 3&lt;/option&gt;
    &lt;/optgroup&gt;
    &lt;optgroup label=&quot;Group 2&quot;&gt;
        &lt;option value=&quot;4&quot;&gt;Option 4&lt;/option&gt;
        &lt;option value=&quot;5&quot;&gt;Option 5&lt;/option&gt;
        &lt;option value=&quot;6&quot;&gt;Option 6&lt;/option&gt;
    &lt;/optgroup&gt;
&lt;/select&gt;