note 104224 modified in function.gettype by danbrown

From: Date: Thu, 02 Jun 2011 17:56:16 +0000
Subject: note 104224 modified in function.gettype by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-180200@lists.php.net to get a copy of this message
This function returns string representation of type. This is generalization of get_class(). I try to order is_* tests by density of occurence, but is bad idea to use result of this call in conditions for performance reasons, usefull and better than gettype for debugging messages. Note, that last line should not be never executed. Also has no sense to make input parameter optional. <?php function get_type($var) { if(is_object($var)) return get_class($var); if(is_null($var)) return 'null'; if(is_string($var)) return 'string'; if(is_array($var)) return 'array'; if(is_int($var)) return 'integer'; if(is_bool($var)) return 'boolean'; if(is_float($var)) return 'float'; if(is_resource($var)) return 'resource'; //throw new NotImplementedException(); return 'unknown'; } ?> --was-- This function returns string representation of type. This is generalization of get_class(). I try to order is_* tests by density of occurence, but is bad idea to use result of this call in conditions for performance reasons, usefull and better than gettype for debugging messages. Note, that last line should not be never executed. Also has no sense to make input parameter optional. <?php function get_type($var) { if(is_object($var)) return get_class($var); if(is_null($var)) return 'null'; if(is_string($var)) return 'string'; if(is_array($var)) return 'array'; if(is_int($var)) return 'integer'; if(is_bool($var)) return 'boolean'; if(is_float($var)) return 'float'; if(is_resource($var)) return 'resource'; //throw new NotImplementedException(); return 'unknown'; } http://php.net/manual/en/function.gettype.php

« previous php.notes (#180200) next »