How can PHP be used to handle arrays of form checkbox values when submitted?

When handling arrays of form checkbox values submitted in PHP, you can use the name attribute of the checkboxes to create an array of values in the $_POST or $_GET superglobals. You can then loop through this array to process each selected checkbox value.

// HTML form with checkboxes
<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" value="Submit">
</form>

// PHP code to handle submitted checkbox values
if(isset($_POST['colors'])){
    $selectedColors = $_POST['colors'];
    foreach($selectedColors as $color){
        echo $color . "<br>";
    }
}