title: PHP check-function for spanish DNI. Server Error 500, why?
body: <p>I am desperate. Since days I am trying to implement a check-function in PHP for the spanish DNI. I am not finding my error. Can someone have a look on it? </p>
  
  <pre><code>&lt;?php
  
  require("bbdd.php");
  
  $errors = '';
  $dni = checkDNI($_POST['dni']);
  
  /**
   * Function which checks if a string is a valid spanish DNI
   *
   * @param string $dniUnchecked
   * @return string $dni
   */      
  function checkDNI($dniUnchecked) {
      //Possible values for the final letter
      $letterValues = array(
          'T' =&gt; 0, 'R' =&gt; 1, 'W' =&gt; 2, 'A' =&gt; 3, 'G' =&gt; 4, 'M' =&gt; 5,
          'Y' =&gt; 6, 'F' =&gt; 7, 'P' =&gt; 8, 'D' =&gt; 9, 'X' =&gt; 10, 'B' =&gt; 11,
          'N' =&gt; 12, 'J' =&gt; 13, 'Z' =&gt; 14, 'S' =&gt; 15, 'Q' =&gt; 16, 'V' =&gt; 17,
          'H' =&gt; 18, 'L' =&gt; 19, 'C' =&gt; 20, 'K' =&gt; 21, 'E' =&gt; 22
      );
      //Check if entered
      if($dniUnchecked == ''){
          $errors .= 'Please enter a DNI.&lt;br/&gt;';  
          echo $errors;
          return false;
      }       
      //Check length
      elseif(strlen($dniUnchecked) != 9){
          $errors .= 'Please enter a DNI that has 8 digits and a check-letter.&lt;br/&gt;';
          echo $errors;
          return false;
      }
      //Check validity
      elseif (preg_match('/^[0-9]{8}[A-Z]$/i', $dniUnchecked)) {
          //Check letter
          $checkArray = str_split($dniUnchecked);
          $checkArray[8] = $letterValues[$checkArray[8]];
          //var_dump($checkArray);
          $checkSum = 0;
          $i = 0;
          $factor = array(7, 3, 1, 7, 3, 1, 7, 3, 1);
          for($i=0; $i&lt;9; $i++){
              $checkArray[$i] = (int)$checkArray[$i];
              $checkSum += ($checkArray[$i] *= $factor[$i]);
          }
          if((preg_match("/\d$/", $checkSum) == 3) {
          //All was ok
          echo 'all ok';
              $dni = trim($dniUnchecked);
              $dni = stripsplashes($dni);
              $dni = htmlspecialchars($dni);
              return $dni;
          } else {
          $errors .= 'Please enter a valid DNI.&lt;br/&gt;';
          echo $errors;
          }
      }
  }
  </code></pre>
  
  <p>When I checkout old commits, then at least the checks if and the first elseif are working. When I implement the second elseif, nothing happens anymore. As you can see I want to see the errors from PHP but there is nothing. Just a 500.
  Would be cool if someone shows me a mistake or shows me the direction. Thanks!</p>
  
  <hr>
  
  <h2>Working Version IMHO</h2>
  
  <pre><code>$dni = checkDNI($_POST['dni']);
  
  /**
   * Function which checks if a string is a valid spanish DNI
   *
   * @param string $dniUnchecked
   * @return string $dni
   */      
  function checkDNI($dniUnchecked) {
      //Error-Output
      $errors = '';
      //Possible values for the final letter
      $letterValues = array(
          'T' =&gt; 0, 'R' =&gt; 1, 'W' =&gt; 2, 'A' =&gt; 3, 'G' =&gt; 4, 'M' =&gt; 5,
          'Y' =&gt; 6, 'F' =&gt; 7, 'P' =&gt; 8, 'D' =&gt; 9, 'X' =&gt; 10, 'B' =&gt; 11,
          'N' =&gt; 12, 'J' =&gt; 13, 'Z' =&gt; 14, 'S' =&gt; 15, 'Q' =&gt; 16, 'V' =&gt; 17,
          'H' =&gt; 18, 'L' =&gt; 19, 'C' =&gt; 20, 'K' =&gt; 21, 'E' =&gt; 22
      );
      //Check if entered
      if($dniUnchecked == ''){
          $errors .= 'Please enter a DNI.&lt;br/&gt;';  
          echo $errors;
          return false;
      }       
      //Check length
      elseif(strlen($dniUnchecked) != 9){
          $errors .= 'Please enter a DNI that has 8 digits and a check-letter.&lt;br/&gt;';
          echo $errors;
          return false;
      }
      //Check validity
      elseif (preg_match('/^[0-9]{8}[A-Z]$/i', $dniUnchecked)) {
          // take numbers as big integer
          $checkNumber = (int)substr($dniUnchecked, 0, 8);
          var_dump($checkNumber);
          // modulo 23 and check if modulo equals corresponding checkletter
          if($checkNumber % 23 == $letterValues[substr($dniUnchecked, 8, 1)]){
          //All was ok
          echo 'all ok';
              $dni = trim($dniUnchecked);
              $dni = stripslashes($dni);
              $dni = htmlspecialchars($dni);
              return $dni;
          } else {
          $errors .= 'Please enter a valid DNI.&lt;br/&gt;';
          echo $errors;
          }
      }
  }
  </code></pre>