What are the best practices for handling multiple checkbox values with the same name in PHP?

When handling multiple checkbox values with the same name in PHP, it is important to use array notation in the name attribute of the checkboxes. This allows PHP to gather all the selected values into an array, making it easier to process them. By using array notation, you can loop through the array of values and perform the necessary operations on each selected checkbox.

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

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