validate_type (listed) 12.11.2018 17:34:19 (35 lines) [Edit] [Raw] [View]

<?php function validate_type(&$variable = null, $types = "string", $fail = null){

//$variable is set
if(isset($variable) || $variable === null){
    //Parse $Types
    if(is_array($types)){ //Type 'Array'
        if(sizeof($types) &gt; 0){
            //Check if the variable matches any given type
            foreach($types as $type){
                if(!is_string($type)){ //Type from 'String' or Type from 'Type'
                    $type = gettype($type);
                }
                if(strtolower(gettype($variable)) === strtolower($type)){
                    return $variable;
                }
            }
        }else{
            if(is_array($variable)){
                return $variable;
            }
        }
    }else{ //Type from 'String' or Type from 'Type'
        //Check if the variable matches the given type
        if(!is_string($types)){
            $types = gettype($types);
        }
        if(strtolower(gettype($variable)) === strtolower($types)){
            return $variable;
        }
    }
}
return $fail;

}