php gettype fix
Following function I created is to store data in a database with the correct type.
I have several questions:
- It makes sense to me to turn empty strings in to NULL. Are there consequences I am not considering?
- What is a good max_length value for sqlite3 text. I know blob and text length constrains are the same, but are there any other considerations like displaying the data in a shell, etc.
- Is there a simple way to check if data is an image, and what type, since mime is not available at this stage?
Does something stick out that I am missing?
public static function getType($value, $max_length = 50){ $type = gettype($value); if($type == 'NULL' || $type == 'boolean' 开发者_StackOverflow || $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'){ return array('type'=>$type,'value'=>$value); }else{ $type = 'blob'; } return array('type'=>$type,'value'=>$value); }
精彩评论