What are some best practices for structuring PHP code to handle multiple dropdown selections in a form?

When handling multiple dropdown selections in a form in PHP, it is best to use arrays to store the selected values. This allows for easy iteration and processing of the selected options. By naming the dropdown inputs as arrays in the HTML form, you can access the selected values in PHP using $_POST or $_GET superglobals.

<form method="post">
  <select name="colors[]" multiple>
    <option value="red">Red</option>
    <option value="blue">Blue</option>
    <option value="green">Green</option>
  </select>
  <input type="submit" value="Submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if(isset($_POST['colors'])){
    foreach ($_POST['colors'] as $color) {
      echo $color . "<br>";
    }
  }
}
?>