What potential pitfalls arise from using multiple fields with the same name in PHP forms?

Using multiple fields with the same name in PHP forms can lead to confusion when processing the form data, as only the last value submitted for that field name will be retained. To avoid this issue, you can use array notation in the field names to ensure that all values are captured as an array in the $_POST or $_GET superglobal.

<form method="post">
  <input type="text" name="colors[]" value="red">
  <input type="text" name="colors[]" value="blue">
  <input type="text" name="colors[]" value="green">
  <input type="submit" value="Submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $colors = $_POST['colors'];
  foreach ($colors as $color) {
    echo $color . "<br>";
  }
}
?>