Validating input against rules I have created a method that validates inputs based on a set of rules. If an error is found, it is put into an array which can later on be used to display the errors to the user.

Below is my code:

register.php

&lt;?php
    require_once('core/init.php');

    if(input::inputExists()){
        $validate = new validate();
        $validate-&gt;checkInput($_POST, array(
            'username' =&gt; array(
                'formatted_name' =&gt; 'username',
                'required' =&gt; 'true',
                'min' =&gt; 4,
                'max' =&gt; 25,
                'unique' =&gt; 'users'
                ),
            'password' =&gt; array(
                'formatted_name' =&gt; 'password',
                'required' =&gt; 'true',
                'min' =&gt; 5
                ),
            'confirm_password' =&gt; array(
                'formatted_name' =&gt; 'password confirmation',
                'matches' =&gt; 'password',
                'required' =&gt; 'true'
                )
        ));
        if ($validate-&gt;validationPassed()) {
            // Register new member...
        } else {
            // Return Errors....
            print_r($validate-&gt;getErrors());
        }
    }

?&gt;

&lt;form method="POST" autocomplete="off"&gt;
    &lt;div class="form-group"&gt;
        &lt;label for="username"&gt;
            Username
        &lt;/label&gt;
        &lt;input type="text" name="username" id="username" /&gt;     
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
        &lt;label for="password"&gt;
            Password
        &lt;/label&gt;
        &lt;input type="password" name="password" id="password" /&gt;     
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
        &lt;label for="confirm_password"&gt;
            Confirm Password
        &lt;/label&gt;
        &lt;input type="password" name="confirm_password" id="confirm_password" /&gt;     
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
        &lt;input type="submit" /&gt;
    &lt;/div&gt;
&lt;/form&gt;


validate.class.php

&lt;?php
class validate {
    private $errors;
    private $passed = 'false';

    // public function __construct(){}

    public function checkInput($data, $items = array()) {
        foreach($items as $item =&gt; $rules){
            foreach($rules as $rule =&gt; $rule_value){
                if ($rule != 'formatted_name'){
                    if ($rule === 'required' &amp;&amp; empty($data[$item])){
                        $this-&gt;addError("{$rules['formatted_name']} is required.");
                    }
                    if ($rule === 'min' &amp;&amp; strlen($data[$item]) &lt; $rule_value){
                        $this-&gt;addError("{$rules['formatted_name']} must be at least {$rule_value} characters long.");
                    }
                    if ($rule === 'max' &amp;&amp; strlen($data[$item]) &gt; $rule_value){
                        $this-&gt;addError("{$rules['formatted_name']} can be a maximum of {$rule_value} characters long.");
                    }
                    if ($rule === 'matches' &amp;&amp; $data[$item] != $rule_value){
                        $this-&gt;addError("{$rules['formatted_name']} does not match {$items[$rule_value]['formatted_name']}");
                    }
                }
            }

        }
    }

    private function addError($error){
        $this-&gt;errors[] = $error;
    }

    public function validationPassed(){
        return empty($this-&gt;errors);
    }

    public function getErrors(){
        return $this-&gt;errors;
    }
}


Any improvements, suggestions or advice you can give me?
Right well first off, I'd consider looking into a framework such as Symfony, Laravel, or some other PHP web framework if you want to get serious about doing web design. Right now there is a lot less baggage in rolling your own framework, and the learning curve can be a little steep on them, but once you understand how they work you can spend way less time writing the framework and much more time implementing the actual features you care about.

There are a lot of suggestions I'd consider making, but most involve using routing/middleware layers to accomplish what you are doing here in order to make that form validation logic re-usable in register.php. The logic itself looks fine, but as your needs grow more and more complex you're going to run into some huge pitfalls with the architecture, or lack thereof, you are currently using.

Either way, I'd consider taking a look at Symfony's Validator component. It does a lot of what you're trying to accomplish, but in a really concise and powerful way and eliminates the need to write all of that rule logic in validate.class.php.

From a stylistic point of view, class names should be uppercased and given a noun not a verb. So your class validate -> Validator. Furthermore, your file names should be named after the class they represent and upper cased so validate.class.php -> Validator.php.

That being said, the code is organized quite nicely and is easy to read. Keep up the good work!