Edit report at https://bugs.php.net/bug.php?id=9654&edit=1
ID: 9654 Updated by: ni...@php.net Reported by: adam at cryptocomm dot com Summary: Add is_type() function -Status: Open +Status: Closed Type: Feature/Change Request Package: *General Issues Operating System: * PHP Version: * -Assigned To: +Assigned To: nikic Block user comment: N Private report: N New Comment: Closing this as PHP already has gettype() for a long, long time and that function can be used for this. http://de.php.net/gettype Previous Comments: ------------------------------------------------------------------------ [2012-05-19 13:36:13] zyss at mail dot zp dot ua Why not to write your own? Note, however, that PHP is slow as mud so even a single function call costs much and it's usually much faster to use native functions for type checking (if you're concerned about the speed). Also it's quite easy to mistype a type :) so using native functions is a preferred option as to me. <?php function is_type($var, $type) { switch ($type) { case 'null': return $var === null; case 'int': case 'integer': case 'long': return is_int($var); case 'float': case 'double': case 'real': return is_float($var); case 'num': case 'numeric': return is_numeric($var); case 'finite': return is_finite($var); case 'nan': return is_nan($var); case 'scalar': return is_scalar($var); case 'res': case 'resource': return is_resource($var); case 'string': return is_string($var); case 'object': return is_object($var); case 'array': return is_array($var); case 'callable': return is_callable($var); case 'bool': case 'boolean': return is_bool($var); case 'dir': return is_dir($var); case 'file': return is_file($var); case 'readable': return is_readable($var); case 'writable': case 'writeable': return is_writable($var); case 'exec': case 'executable': return is_executable($var); case 'link': case 'symlink': return is_link($var); case 'uploaded': case 'uploaded_file': return is_uploaded_file($var); case 'soap_fault': return is_soap_fault($var); } return false; } /******* TEST CODE *******/ echo 'null(1): ', is_type(null, 'null') ? 'Y' : 'N', "\n"; echo 'int(1): ', is_type(1, 'int') ? 'Y' : 'N', "\n"; echo 'integer(1): ', is_type(1, 'integer') ? 'Y' : 'N', "\n"; echo 'long(1): ', is_type(1, 'long') ? 'Y' : 'N', "\n"; echo 'float(1.0): ', is_type(1.0, 'float') ? 'Y' : 'N', "\n"; echo 'double(1.0): ', is_type(1.0, 'double') ? 'Y' : 'N', "\n"; echo 'real(1.0): ', is_type(1.0, 'real') ? 'Y' : 'N', "\n"; $f = fopen("http://www.example.com/", "r"); echo 'res($f): ', is_type($f, 'res') ? 'Y' : 'N', "\n"; echo 'resource($f): ', is_type($f, 'resource') ? 'Y' : 'N', "\n"; fclose($f); echo 'string("abc"): ', is_type('abc', 'string') ? 'Y' : 'N', "\n"; echo 'object(stdClass): ', is_type(new stdClass(), 'object') ? 'Y' : 'N', "\n"; echo 'array([1, 2, 3]): ', is_type(array(1, 2, 3), 'array') ? 'Y' : 'N', "\n"; echo 'callable(Closure): ', is_type(function() { }, 'callable') ? 'Y' : 'N', "\n"; echo 'bool(true): ', is_type(true, 'bool') ? 'Y' : 'N', "\n"; echo 'boolean(true): ', is_type(true, 'boolean') ? 'Y' : 'N', "\n"; echo 'dir(__DIR__): ', is_type(__DIR__, 'dir') ? 'Y' : 'N', "\n"; echo 'file(__FILE__): ', is_type(__FILE__, 'file') ? 'Y' : 'N', "\n"; echo 'readable(__FILE__): ', is_type(__FILE__, 'readable') ? 'Y' : 'N', "\n"; echo 'writable(__FILE__): ', is_type(__FILE__, 'writable') ? 'Y' : 'N', "\n"; echo 'writeable(__FILE__): ', is_type(__FILE__, 'writeable') ? 'Y' : 'N', "\n"; $php = $_SERVER['argv'][0]; echo 'exec($php): ', is_type($php, 'exec') ? 'Y' : 'N', "\n"; echo 'executable($php): ', is_type($php, 'executable') ? 'Y' : 'N', "\n"; $link = __FILE__ . '.symlink'; if (function_exists('symlink')) @symlink(__FILE__, $link); echo 'link($link): ', is_type($link, 'link') ? 'Y' : 'N', "\n"; @unlink($link); $upl = @$_FILES['file']['tmp_name']; echo 'uploaded(__FILE__): ', is_type(__FILE__, 'uploaded') ? 'Y' : 'N', "\n"; echo 'uploaded_file(__FILE__): ', is_type(__FILE__, 'uploaded_file') ? 'Y' : 'N', "\n"; $client = new SoapClient('http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl', array('exceptions' => 0)); $result = $client->SomeFunction(); echo 'soap_fault($result): ', is_type($result, 'soap_fault') ? 'Y' : 'N', "\n"; ------------------------------------------------------------------------ [2001-03-09 09:57:24] adam at cryptocomm dot com It would be much more clean and quick to consolidate all the "is" functions into like is_type($variable, "int") and have it return the true or false based on if the value of $variable is the datatype you specified as the second paramter. This makes more sense to me then having like 20 is_whatever functions. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=9654&edit=1