title: Invalid argument in PHP form - foreach()
body: <p>I keep getting a "Warning: Invalid argument supplied for foreach() on line 102" message when I test my PHP contact form. I have rebuilt my website recently, but I am using the exact same PHP contact form code that I used on my old website, and the old website's form still works without error. I have copied &amp; pasted it word for word, so I'm not sure why I'm getting this error. Any help would really be appreciated.</p>
  
  <p>Here is the line that I am getting the supposed error on (line 102):</p>
  
  <pre><code>foreach($errors as $value) {
  </code></pre>
  
  <p>Here is the entire form script (the error line is right at the bottom):</p>
  
  <pre><code>&lt;?php 
  
  // 
  if(isset($_POST["submit"]))
  {
  
      //This is checking if the string length in the firstname field was greater than 1 character
      if(strlen($_POST["firstname"]) &gt; 1)
      {
  
          $firstname = $_POST["firstname"];
          //echo $firstname;
      }
  
      else
      {
          //echo "You did not type in first name";
          $errors["firstname"] = "&lt;span class=\"error\"&gt;You did not type in a first name.&lt;/span&gt;";
      }
  
  
      if(strlen($_POST["lastname"]) &gt; 1)
      {
  
          $lastname = $_POST["lastname"];
      }
  
      else
      {
          $errors["lastname"] = "&lt;span class=\"error\"&gt;You did not type in a last name.&lt;/span&gt;";
      }
  
  
      if(strlen($_POST["email"]) &gt; 1)
      {
  
          $email = $_POST["email"];
  
          if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
              $email = $email;
          }
  
          else {
              $errors["email"] = "&lt;span class=\"error\"&gt;Email is invalid.&lt;/span&gt;";
          }
      }
  
      else
      {
          $errors["email"] = "&lt;span class=\"error\"&gt;You did not type in an email address.&lt;/span&gt;";
      }
  
      //This is checking if the string length in the message field was greater than 1 character
      if(strlen($_POST["message"]) &gt; 1)
      {
  
          $message = $_POST["message"];
          //echo $firstname;
      }
  
      else
      {
  
          $errors["message"] = "&lt;span class=\"error\"&gt;You did not type in a message.&lt;/span&gt;";
      }
  
  
      // Code to tell form NOT to send form if there are any errors.
      if($errors &lt; 1)
      {
              $to = "example@email.com";
              $from = $email;
              // headers makes sure you have a reply-to address
              $headers = "From: {$from}" . "\r\n";
              $headers .= 'MIME-Version: 1.0' . "\r\n";
              $headers .= 'Content-type: text/html; charset=iso-8859-1';
  
              $subject =  
                  "From: ($from)" . "&lt;br /&gt;" .
                  "First name: ($firstname)" . "&lt;br /&gt;" .
                  "Last name: ($lastname)" . "&lt;br /&gt;" .
                  "Email: ($email)" . "&lt;br /&gt;" .
                  "Message: ($message)";
  
              if(mail($to, $from, $subject, $headers))
              {
                  $finalMsg = "&lt;p class=\"success\"&gt;Thank you! Your email was sent.&lt;/p&gt;";
              }
  
              else {
                  $finalMsg = "&lt;p class=\"error\"&gt;Your email was NOT sent. Please try again.&lt;/p&gt;";
              }
  
  }
  
  ?&gt;
  
  &lt;?php
  
  //each error is displayed
  
  foreach($errors as $value) {
  
  echo "&lt;span&gt;$value&lt;/span&gt;&lt;br /&gt;";
  
  }
      }
  ?&gt;
  </code></pre>