PHP Validation with functions and list() I have multiple form validation checks around my site so I decided to write them as functions to simplify the process of creating the validation pages, here is a snippet for an understanding of what I'm doing.

// Declare regex variables

   $regex_email = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
   $regex_password = "/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*\W).*$/";

// Declare validation functions

    function checkEmail($regex, $email) {

        if (!preg_match($regex, $email)) {

            $error = "The format of that Email Address is invalid";
            $solution = "Please go back and &lt;a href=\"javascript:history.go(-1)\"&gt;try again&lt;/a&gt;";

        }

        return array($error, $solution);

    }

    function checkPassword($regex, $password) {

        if (!preg_match($regex, $password)) {

            $error = "Your Passwords does not meet our requirements";
            $solution = "Please go back and &lt;a href=\"javascript:history.go(-1)\"&gt;try again&lt;/a&gt;";

        }

        return array($error, $solution);

    }


I was hoping that when I come to validate it I could simply use:

// Values for $email and $password are fetched from `$_POST` before hand...

// Validate form

    list ($error, $solution) = checkEmail($regex_email, $email);
    list ($error, $solution) = checkPassword($regex_password, $password);

// Display error

    echo("Error:&lt;br /&gt;".$error."&lt;br /&gt;Solution:&lt;br /&gt;".$solution."");


But alas, it doesn't work and no errors at all are displayed and I'm having to do error checks after each one like this:

// Validate form

    list ($error, $solution) = checkEmail($regex_email, $email);

    if (!$error) {

        list ($error, $solution) = checkPassword($regex_password, $password);

    }

// Display error

    echo("Error:&lt;br /&gt;".$error."&lt;br /&gt;Solution:&lt;br /&gt;".$solution."");


I don't mind doing it like this, but when I have say 10+ check*VALUE* functions being called, each one wrapped in an if statement within an if statement and so on, it looks a little messy, I wasn't sure if there was a different approach which would work more like the first way I tried without all the if statement's.
This might not be what you are looking for but I recently wrote this code to validate my forms.

As you might notice I define the data to be validated in the $validate array. Then some loops run through them and show the proper errors. If no errors are found, it sends the form.

With some modifications, you could probably use something similar. Hope it helps.

&lt;?php

    # post data collection
    $name   = "Full name";
    $email  = "a@b.c";
    $age    = "193";
    $x      = "s";
    $y      = "s";

    # select data that needs validation
    $validate   = array(
        'required'      =&gt; array($name, $email, $x, $y),
        'validEmail'    =&gt; array($email),
        'validNumber'   =&gt; array($age),
        'validAlpha'    =&gt; array($name)
    );

    # error messages
    $errorsMsgs = array(
        'required'      =&gt; 'Please fill out all required fields.',
        'validEmail'    =&gt; 'is an invalid email address.',
        'validNumber'   =&gt; 'is an invalid number.',
        'validAlpha'    =&gt; 'contains invalid characters. This field only accepts letters and spaces.'
    );

    $errorMarkup    = "&lt;h1&gt;We found a few errors :-(&lt;/h1&gt;&lt;h2&gt;Please fix these errors and try again&lt;/h2&gt;&lt;ol&gt;";
    $successMarkup  = "&lt;h1&gt;Success!&lt;/h1&gt;&lt;h2&gt;Your form was sent successfully.&lt;/h2&gt;";
    $backMarkup     = "&lt;a href=\"" . $_SERVER['HTTP_REFERER'] . "\"&gt;Back to form&lt;/a&gt;";

    # begin state
    $valid = true;

    # loop through fields of error types
    foreach ($validate as $type =&gt; $fields) {
        # loop through values of fields to be tested
        foreach ($fields as $value) {
            # throw error if value is required and not entered
            if ($type === 'required' &amp;&amp; strlen($value) === 0) {
                $errorMarkup .= "&lt;li&gt;$errorsMsgs[$type]&lt;/li&gt;";
                $valid = false;
                break;
            }
            else if (
                        $type === 'validEmail'  &amp;&amp; !filter_var($value, FILTER_VALIDATE_EMAIL) ||
                        $type === 'validNumber' &amp;&amp; !preg_match('/^[0-9 ]+$/', $value) ||
                        $type === 'validAlpha'  &amp;&amp; !preg_match('/^[a-zA-Z ]+$/', $value)
                    ) {
                if (strlen($value) === 0) {break;} # skip check if value is not entered
                $errorMarkup .= "&lt;li&gt;\"$value\" $errorsMsgs[$type]&lt;/li&gt;";
                $valid = false;
                continue;
            }
        }
    }

    if ($valid) {
        # email form
        $body = $successMarkup . $backMarkup;
        $title = "Form sent";
    } else {
        $body = $errorMarkup . "&lt;/ol&gt;" . $backMarkup;
        $title = "Form errors";
    }

    # write html ouput
    echo "&lt;!DOCTYPE html&gt;&lt;head&gt;&lt;title&gt;$title&lt;/title&gt;&lt;style type=\"text/css\"&gt;body{margin:100px;font:16px/1.5 sans-serif;color:#111}h1{font-size:32px;margin:0;font-weight:bold}h2{font-size:18px;margin:0 0 20px 0}ol,li{list-style-position:inside;padding-left:0;margin-left:0}&lt;/style&gt;&lt;/head&gt;&lt;body&gt;$body&lt;/body&gt;&lt;/html&gt;";
?&gt;