title: In PHP, what does this block of code do in forms required field?
body: <p><strong>What does this block of PHP code do?</strong></p>
  
  <pre><code>function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
  }
  </code></pre>
  
  <p><strong>This is the entire PHP Program</strong></p>
  
  <p><em>It is taken from w3schools.com
  <a href="http://www.w3schools.com/php/showphp.asp?filename=demo_form_validation_required" rel="nofollow">http://www.w3schools.com/php/showphp.asp?filename=demo_form_validation_required</a></em></p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;  
  &lt;html&gt;
  &lt;head&gt;
  &lt;style&gt;
  .error {color: #FF0000;}
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;  
  
  &lt;?php
  // define variables and set to empty values
  $nameErr = $emailErr = $genderErr = $websiteErr = "";
  $name = $email = $gender = $comment = $website = "";
  
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
      $nameErr = "Name is required";
    } else {
      $name = test_input($_POST["name"]);
    }
  
    if (empty($_POST["email"])) {
      $emailErr = "Email is required";
    } else {
      $email = test_input($_POST["email"]);
    }
  
    if (empty($_POST["website"])) {
      $website = "";
    } else {
      $website = test_input($_POST["website"]);
    }
  
    if (empty($_POST["comment"])) {
      $comment = "";
    } else {
      $comment = test_input($_POST["comment"]);
    }
  
    if (empty($_POST["gender"])) {
      $genderErr = "Gender is required";
    } else {
      $gender = test_input($_POST["gender"]);
    }
  }
  
  function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
  }
  ?&gt;
  
  &lt;h2&gt;PHP Form Validation Example&lt;/h2&gt;
  &lt;p&gt;&lt;span class="error"&gt;* required field.&lt;/span&gt;&lt;/p&gt;
  &lt;form method="post" action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?&gt;"&gt;  
    Name: &lt;input type="text" name="name"&gt;
    &lt;span class="error"&gt;* &lt;?php echo $nameErr;?&gt;&lt;/span&gt;
    &lt;br&gt;&lt;br&gt;
    E-mail: &lt;input type="text" name="email"&gt;
    &lt;span class="error"&gt;* &lt;?php echo $emailErr;?&gt;&lt;/span&gt;
    &lt;br&gt;&lt;br&gt;
    Website: &lt;input type="text" name="website"&gt;
    &lt;span class="error"&gt;&lt;?php echo $websiteErr;?&gt;&lt;/span&gt;
    &lt;br&gt;&lt;br&gt;
    Comment: &lt;textarea name="comment" rows="5" cols="40"&gt;&lt;/textarea&gt;
    &lt;br&gt;&lt;br&gt;
    Gender:
    &lt;input type="radio" name="gender" value="female"&gt;Female
    &lt;input type="radio" name="gender" value="male"&gt;Male
    &lt;span class="error"&gt;* &lt;?php echo $genderErr;?&gt;&lt;/span&gt;
    &lt;br&gt;&lt;br&gt;
    &lt;input type="submit" name="submit" value="Submit"&gt;  
  &lt;/form&gt;
  
  &lt;?php
  echo "&lt;h2&gt;Your Input:&lt;/h2&gt;";
  echo $name;
  echo "&lt;br&gt;";
  echo $email;
  echo "&lt;br&gt;";
  echo $website;
  echo "&lt;br&gt;";
  echo $comment;
  echo "&lt;br&gt;";
  echo $gender;
  ?&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>