en Home Creating models for the Zend Framework from command line via phpcli Messenger Laravel Framework Load functions and array's Zend ACL num_row with PDO Autoload Multiple PHP installations e() for echo Read Folder PHP goto statement

fixing php's gettype function

<?php    

public static function getType($value, $max_length = 50){

    $type = gettype($value);

    if($type == 'NULL'
            || $type == 'boolean'
            || $type == 'integer'
            || $type == 'double'
            || $type == 'object'
            || $type == 'resource'
            || $type == 'array'
        )
        return array('type'=>$type,'value'=>$value);

    if($type == 'string' && empty($value))
        return array('type'=>'NULL','value'=>$value);

    if($type == 'string' && strlen($value) > $max_length)
        return array('type'=>'blob','value'=>$value);

    if($type == 'string' && substr($value, 0,1) === '0')
        return array('type'=>'string','value'=>$value);

    if($type == 'string' && is_numeric($value)){
        $int   = (int) $value;
        $float = (float) $value;

        if($int == $value){
            $value = $int;
            $type = 'integer';
        }elseif($float == $value){
            $value = $float;
            $type = 'double';
        }
    }elseif($type == 'string'){
        $type = 'string';
    }else{
        $type = 'blob';
    }
    return array('type'=>$type,'value'=>$value);
}
    
?>

Tags: code php


First Created: 2011-06-17T10:10:09Z
Last Modified: 2026-04-05 19:35