What is the recommended method for handling multiple checkboxes with the same name in PHP forms?

When dealing with multiple checkboxes with the same name in PHP forms, it is recommended to use an array as the name attribute for the checkboxes. This allows you to easily loop through the values in the PHP code and process them accordingly.

<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 $color) {
      echo $color . "<br>";
    }
  }
}
?>