php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #9654 Add is_type() function
Submitted: 2001-03-09 09:57 UTC Modified: 2012-08-10 18:48 UTC
Votes:3
Avg. Score:4.3 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: adam at cryptocomm dot com Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: * OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: adam at cryptocomm dot com
New email:
PHP Version: OS:

 

 [2001-03-09 09:57 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-14 11:31 UTC] jani@php.net
-Summary: is_type consolidation +Summary: Add is_type() function -Package: Feature/Change Request +Package: *General Issues -Operating System: FreeBSD 4.2 +Operating System: * -PHP Version: 4.0.4pl1 +PHP Version: *
 [2012-05-19 13:36 UTC] 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";
 [2012-08-10 18:48 UTC] nikic@php.net
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
 [2012-08-10 18:48 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC