How can PHP be used to handle multiple checkbox selections in a form?

When handling multiple checkbox selections in a form using PHP, you can use an array as the name attribute for the checkboxes. This way, when the form is submitted, PHP will receive an array of values corresponding to the selected checkboxes. You can then loop through this array to process each selected checkbox individually.

<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(!empty($_POST['colors'])){
        foreach($_POST['colors'] as $selected){
            echo $selected . "<br>";
        }
    }
}
?>