What is the best practice for handling multiple checkbox values in PHP forms?

When handling multiple checkbox values in PHP forms, it is important to use an array for the checkbox input field name in the HTML form. This allows you to receive multiple selected values as an array in the PHP script. You can then loop through the array to process each selected checkbox value individually.

// HTML form with checkbox inputs
<form method="post" action="process.php">
    <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" value="Submit">
</form>

// PHP script to handle the form submission
<?php
if(isset($_POST['colors'])) {
    $selectedColors = $_POST['colors'];
    
    foreach($selectedColors as $color) {
        echo "Selected color: " . $color . "<br>";
    }
}
?>