PHP Validation with functions and list()
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;