PHP检查对象是否可以转换为整数?

Nik*_*s R 7 php int type-conversion

在PHP中,似乎每个对象都可以通过调用转换为整数intval($object),但这不是我想要的.我想要的是,检查对象是否有效转换为人类认为的整数.

即,有效的对象将是

  • 12
  • 12.0
  • "12"
  • "12.0"

而且无效

  • MyFooInstance()
  • "some string"
  • "12.0.0"
  • "0 12.0"

在python中,我可以简单地进行以下操作:

try:
    int(var)
except (TypeError, ValueError):
    return False
return True
Run Code Online (Sandbox Code Playgroud)

我怎样才能在PHP中实现这一点?

Dog*_*ert 12

使用is_numeric.

<?php
$tests = array(
    "42", 
    1337, 
    "1e4", 
    "not numeric", 
    array(), 
    9.1
);

foreach ($tests as $element) {
    if (is_numeric($element)) {
        echo "'{$element}' is numeric", PHP_EOL;
    } else {
        echo "'{$element}' is NOT numeric", PHP_EOL;
    }
}
?>


'42' is numeric
'1337' is numeric
'1e4' is numeric
'not numeric' is NOT numeric
'Array' is NOT numeric
'9.1' is numeric
Run Code Online (Sandbox Code Playgroud)

(来自页面)


biz*_*lop 5

整数(不仅仅是数字)测试:http: //codepad.org/3E8IYHKY

function to_int_or_null( $v ){
  if( is_int(     $v ))  return $v; 
  if( is_float(   $v ))  return $v === (float)(int)$v  ?  (int)$v  :  null;
  if( is_numeric( $v ))  return to_int_or_null( +$v );
  return null;
}
Run Code Online (Sandbox Code Playgroud)

结果:

int(1)                                  int(1)
float(1)                                int(1)
float(-0)                               int(0)
string(2) "-1"                          int(-1)
string(2) "+1"                          int(1)
string(1) "1"                           int(1)
string(2) " 1"                          int(1)
string(2) "01"                          int(1)
string(3) " 01"                         int(1)
string(4) " -01"                        int(-1)
string(3) "1e0"                         int(1)
string(4) "1.00"                        int(1)
string(18) "1.0000000000000001"         int(1)
string(18) "0.0000000000000001"         NULL
string(17) "1.000000000000001"          NULL
string(4) "1.11"                        NULL
string(4) "1e40"                        NULL
string(6) "1e9999"                      NULL
float(1.1100000000000000977)            NULL
float(1.0000000000000000304E+40)        NULL
float(INF)                              NULL
string(4) "0xFF"                        NULL or int(255) !!!
string(6) "0b1111"                      NULL
string(5) "123  "                       NULL
string(0) ""                            NULL
string(2) "  "                          NULL
string(6) "123foo"                      NULL
string(6) "foo456"                      NULL
string(3) "foo"                         NULL
bool(true)                              NULL
bool(false)                             NULL
NULL                                    NULL
array(0) {}                             NULL
object(stdClass)#7 (0) {}               NULL
Run Code Online (Sandbox Code Playgroud)

旧的,错误的回答 http://codepad.org/LoqfAgNl
失败,整数值浮点类型:(double)123

function is_integerable( $v ){
  return is_numeric($v) && +$v === (int)(+$v);
}
Run Code Online (Sandbox Code Playgroud)