What are the potential pitfalls of using checkboxes in PHP for multiple selections?

One potential pitfall of using checkboxes in PHP for multiple selections is that unchecked checkboxes do not get submitted with the form data. To solve this issue, you can use an array as the name attribute for the checkboxes. This way, all checkbox values, whether checked or unchecked, will be submitted as an array in the form data.

<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(isset($_POST['colors'])) {
    $selected_colors = $_POST['colors'];
    foreach($selected_colors as $color) {
      echo $color . "<br>";
    }
  }
}
?>