PHPの型チェックのメモ

テストコード

$f = tempnam(‘/tmp/’,’test’);
$testme = array(
null,
true,
false,
array(),
0,
1,
0.10,
1.123,
”,
‘0’,
‘1’,
’10’,
‘0.10’,
‘1.123’,
‘1,234,567’,
‘ 1’,
‘1 ‘,
‘ 1 ‘,
‘Hello’,
new stdClass(),
fopen($f,’w’),
);

$result = array();
foreach($testme as $value){

$tmp = array();
$tmp[‘value’] = $value;

$tmp[‘isset’] = isset($value);
$tmp[‘null’] = is_null($value);
$tmp[’empty’] = empty($value);

$tmp[‘bool’] = is_bool($value);

$tmp[‘array’] = is_array($value);

$tmp[‘string’] = is_string($value);

$tmp[‘int’] = is_int($value);
$tmp[‘float’] = is_float($value);
$tmp[‘numeric’] = is_numeric($value);

$tmp[‘scalar’] = is_scalar($value);

$tmp[‘object’] = is_object($value);
$tmp[‘resource’] = is_resource($value);

$tmp[‘true’] = ($value) ? true : false;
$tmp[‘false’] = (!$value) ? true : false;

$result[] = $tmp;

}
fclose($value);
@unlink($f);

var_dump($result);

結果

array (size=21)
  0 => 
    array (size=15)
      'value' => null
      'isset' => boolean false
      'null' => boolean true
      'empty' => boolean true
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean false
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean false
      'scalar' => boolean false
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean false
      'false' => boolean true
  1 => 
    array (size=15)
      'value' => boolean true
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean false
      'bool' => boolean true
      'array' => boolean false
      'string' => boolean false
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean false
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean true
      'false' => boolean false
  2 => 
    array (size=15)
      'value' => boolean false
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean true
      'bool' => boolean true
      'array' => boolean false
      'string' => boolean false
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean false
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean false
      'false' => boolean true
  3 => 
    array (size=15)
      'value' => 
        array (size=0)
          empty
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean true
      'bool' => boolean false
      'array' => boolean true
      'string' => boolean false
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean false
      'scalar' => boolean false
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean false
      'false' => boolean true
  4 => 
    array (size=15)
      'value' => int 0
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean true
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean false
      'int' => boolean true
      'float' => boolean false
      'numeric' => boolean true
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean false
      'false' => boolean true
  5 => 
    array (size=15)
      'value' => int 1
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean false
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean false
      'int' => boolean true
      'float' => boolean false
      'numeric' => boolean true
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean true
      'false' => boolean false
  6 => 
    array (size=15)
      'value' => float 0.1
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean false
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean false
      'int' => boolean false
      'float' => boolean true
      'numeric' => boolean true
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean true
      'false' => boolean false
  7 => 
    array (size=15)
      'value' => float 1.123
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean false
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean false
      'int' => boolean false
      'float' => boolean true
      'numeric' => boolean true
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean true
      'false' => boolean false
  8 => 
    array (size=15)
      'value' => string '' (length=0)
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean true
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean true
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean false
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean false
      'false' => boolean true
  9 => 
    array (size=15)
      'value' => string '0' (length=1)
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean true
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean true
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean true
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean false
      'false' => boolean true
  10 => 
    array (size=15)
      'value' => string '1' (length=1)
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean false
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean true
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean true
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean true
      'false' => boolean false
  11 => 
    array (size=15)
      'value' => string '10' (length=2)
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean false
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean true
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean true
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean true
      'false' => boolean false
  12 => 
    array (size=15)
      'value' => string '0.10' (length=4)
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean false
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean true
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean true
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean true
      'false' => boolean false
  13 => 
    array (size=15)
      'value' => string '1.123' (length=5)
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean false
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean true
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean true
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean true
      'false' => boolean false
  14 => 
    array (size=15)
      'value' => string '1,234,567' (length=9)
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean false
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean true
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean false
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean true
      'false' => boolean false
  15 => 
    array (size=15)
      'value' => string ' 1' (length=2)
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean false
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean true
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean true
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean true
      'false' => boolean false
  16 => 
    array (size=15)
      'value' => string '1 ' (length=2)
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean false
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean true
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean false
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean true
      'false' => boolean false
  17 => 
    array (size=15)
      'value' => string ' 1 ' (length=3)
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean false
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean true
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean false
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean true
      'false' => boolean false
  18 => 
    array (size=15)
      'value' => string 'Hello' (length=5)
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean false
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean true
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean false
      'scalar' => boolean true
      'object' => boolean false
      'resource' => boolean false
      'true' => boolean true
      'false' => boolean false
  19 => 
    array (size=15)
      'value' => 
        object(stdClass)[7]
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean false
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean false
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean false
      'scalar' => boolean false
      'object' => boolean true
      'resource' => boolean false
      'true' => boolean true
      'false' => boolean false
  20 => 
    array (size=15)
      'value' => resource(10, Unknown)
      'isset' => boolean true
      'null' => boolean false
      'empty' => boolean false
      'bool' => boolean false
      'array' => boolean false
      'string' => boolean false
      'int' => boolean false
      'float' => boolean false
      'numeric' => boolean false
      'scalar' => boolean false
      'object' => boolean false
      'resource' => boolean true
      'true' => boolean true
      'false' => boolean false

@ Mac PHP 5.3.27