How can PHP interpret and extract data from checkboxes with multiple values?

When dealing with checkboxes that have multiple values, PHP can interpret and extract the data by using an array as the name attribute for the checkboxes in the HTML form. This way, when the form is submitted, PHP will receive an array of values corresponding to the checked checkboxes.

// 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 extract data from checkboxes
if(isset($_POST['colors'])){
    $selectedColors = $_POST['colors'];
    
    foreach($selectedColors as $color){
        echo $color . "<br>";
    }
}