title: Contact Form - How to display the contents of error message array in body of the form's html
body: <p>This is my first time trying to use php in a website and posting a question to stackoverflow. I hope I haven't posted too much code.</p>
  
  <p>The goal is to have the results of a <code>foreach</code> loop display an array's content in the form's html. I'm have been able to display one error message, but not more than one when several exist. Currently the code returns 'Array' when submitted with errors.</p>
  
  <pre><code>   &lt;?php
  $to = "companyname@email.com";
  $subject = "Contact Form";
  
  if (isset($_POST["submit"])) {
      $nam = strip_tags($_POST["name"]);
      $ema = htmlspecialchars($_POST['email']);
      $msg = strip_tags($_POST["message"]);
  
  $error_msg=array(); //initiate array, not required
  
  if (empty($nam) || !preg_match("/^[\s.'\-\pL]{1,60}$/u", $nam)) { 
  $error_msg[] = "&lt;p&gt;Please fill in your name with only letters, spaces and    basic punctuation (.&amp;nbsp;-&amp;nbsp;')&lt;/p&gt;";
  }
  
  if (empty($ema) || !filter_var($ema, FILTER_VALIDATE_EMAIL)) {
      $error_msg[] = "&lt;p&gt;Your email must have a valid format, such as emailname@mailhost.com&lt;/p&gt;";
  }
  
  $limit = 1500; 
  
  if (empty($msg) || !preg_match("/^[0-9\/\-\_\s'\(\)\@!\?\.,:;\pL]+$/u", $msg) || (strlen($msg) &gt; $limit)) { 
  $error_msg[] = "&lt;p&gt;The Message field must contain only letters, digits, spaces and basic punctuation (&amp;nbsp;'&amp;nbsp;-&amp;nbsp;,&amp;nbsp;.&amp;nbsp;:&amp;nbsp;;&amp;nbsp;/ and parentheses), and has a limit of 1500 characters&lt;/p&gt;";
  }
  
  $body = 
      "Name of sender: $nam\n\n" .
      "Email of sender: $ema\n\n" .
      "Message:\n\n" .
      "$msg" ; 
  
  $body = wordwrap($body, 70);
  
  $headers = "Reply-To: $nam &lt;$ema&gt;\r\n";
  $headers .= "Return-Path: &lt;$ema&gt;\r\n";
  $headers .= "From: $nam &lt;$ema&gt;\r\n";
  $headers .= "MIME-Version: 1.0\r\n";
  $headers .= "X-Sender: $ema\r\n";
  $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
  $headers .= "X-Priority: 3\r\n";
  $headers .= "X-Mailer: PHP" . phpversion() . "\r\n";
  
  if ($error_msg) {
  
  foreach ($error_msg as $error_output) {
  
  $error_msg_list .= '&lt;div class="error"&gt;' . $error_output . '&lt;/div&gt;';
  
  }
  // if no errors, send the email
  } else if (!$error_msg) {
  mail ($to, $subject, $body, $headers);
  $success_output = "Your message has been sent. We will get back to you shortly.";
  $_POST = array();  //clears the form data after valid form submission
  } 
  } //end isset
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;title&gt;Contact Us&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div class="content"&gt;
      &lt;h1&gt;Contact Us&lt;/h1&gt;
      &lt;form method="post" action="index.php"&gt;
      &lt;?php
        if (isset($error_msg_list)) { 
         echo  $error_msg_list;   
         } else { 
          echo "&lt;div class='success'&gt;" . $success_output . "&lt;/div&gt;";} 
      ?&gt;
          &lt;div&gt;
              &lt;label for="name"&gt;Name&lt;/label&gt;
              &lt;input name="name" type="text" id="name" placeholder="your name" value="&lt;?php if (isset($_POST["name"])) {echo $nam;} ?&gt;"&gt;      
          &lt;/div&gt;
          &lt;div&gt;
              &lt;label for="email"&gt;Email Address&lt;/label&gt;
              &lt;input name="email" type="email" id="email" placeholder="your email address" value="&lt;?php if (isset($_POST["email"])) {echo $ema;} ?&gt;"&gt;
          &lt;/div&gt;      
          &lt;div&gt;
              &lt;label for="message"&gt;Message&lt;/label&gt;
              &lt;textarea name="message" rows="10" id="message"&gt;&lt;?php if (isset($_POST["message"])) {echo $msg;} ?&gt;&lt;/textarea&gt;
          &lt;/div&gt;              
          &lt;div&gt;
              &lt;input type="submit" name="submit" value="Send"&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>