If no messages were found in array then also it is returning 2 instead of null PHP
<p>If no messages were found in an array then it is returning 2 instead of null PHP</p>

<p>I am appending messages to array and calling functions to append array but while calling the functions there were no messages found in array also it is showing them as 2 value those are Array ( [0] => [1] => ) </p>

<pre><code>$errorMessages = array ();

    $isError = false;
    $middleName = $_POST [ 'middleName' ];
    $middleName = trim( stripslashes( $middleName ) );

    //validation for warrant id
    $warrantId = $_POST [ 'warrantId' ];
    if ( $warrantId == null || ( strlen( $warrantId ) ) &lt;= 0 ) {
        $errorMessages[] = "Warrant Id is required";
    }  else {
        $message = 'enter keyboard characters only for warrant Id.';
        $warrantId = trim( stripslashes( $warrantId ) );
        $x = checkLength($warrantId, 'WarrantId', 1);
        $errorMessages[] = $x;

        //$errorMessages[] = checkRegEx( $warrantId, '/^([a-zA-Z0-9._\- #,^&amp;`~&lt;&gt;:!@$(){}\"\';\*\[\]?%| \n \r \t]*)$/', $message );
    }

    $errorMsg = count($errorMessages);
    print_r($errorMsg);
</code></pre>

<p>functions   </p>

<pre><code>/*
* Checks the field length is not greater than allowed length
* @params unknown values $fieldValue, $fieldName, $maxLength
* @return tables rows $rowResponse
*/
function checkLength($fieldValue, $fieldName, $maxLength) { 
    $errorMsg = NULL;
if (strlen ( $fieldValue ) &gt; $maxLength) {

    $errorMsg = $fieldName . " cannot be greater than " . $maxLength . " characters.";
}   
return $errorMsg;   
}

/*
* Checks the given field value to match with regular expression or not
* @params unknown values $fieldValue, $mask, $message
* @return tables rows $rowResponse
*/
function checkRegEx($fieldValue, $regEx, $message) {
    $errorMsg = NULL;
if (! (preg_match ( $regEx, $fieldValue ))) {
    $errorMsg =  $message;
}

return $errorMsg;   
}
</code></pre>

<p>if no messages are found in array then also it is returning it as 2 </p>
