Are there any best practices for handling multiple select fields in PHP forms?

When handling multiple select fields in PHP forms, it is best practice to use an array as the name attribute for the select field. This allows you to easily access the selected values as an array in the PHP script.

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

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