简体   繁体   English

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

[英]PHP check if object can be converted to integer?

In PHP, it seems like every object can be converted to an integer, just by calling intval($object) , but this is not what I want. 在PHP中,似乎每个对象都可以通过调用intval($object)转换为整数,但这不是我想要的。 What I want is, to check if the object would be valid to be converted into an integer for what a human thinks it is. 我想要的是,检查对象是否有效转换为人类认为的整数。

Ie, valid objects would be 即,有效的对象将是

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

And not valid would be 而且无效

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

etc. In python, I could simply to the following: 在python中,我可以简单地进行以下操作:

try:
    int(var)
except (TypeError, ValueError):
    return False
return True

How can I achive this in PHP? 我怎样才能在PHP中实现这一点?

Use is_numeric . 使用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

(From the page) (来自页面)

Integer (not just numeric) test: http://codepad.org/3E8IYHKY 整数 (不仅仅是数字)测试: 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;
}

Results: 结果:

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

Old, buggy answer http://codepad.org/LoqfAgNl 老,越野车回答 http://codepad.org/LoqfAgNl
Fails with integer-valued float type: (double)123 失败的整数值浮点类型: (double)123

function is_integerable( $v ){
  return is_numeric($v) && +$v === (int)(+$v);
}

See PHP's ctype_digit() . 请参阅PHP的ctype_digit()

This function evaluates a string to see if all character are numeric. 此函数计算字符串以查看是否所有字符都是数字。 Thus "1.1" will not return true because "." 因此"1.1"不会返回true因为"." is not numeric, but "11" will. 不是数字,但"11"将。 Also note this works for strings only, so numbers without the surrounding quotation marks will also not work. 另请注意,这仅适用于字符串,因此没有周围引号的数字也不起作用。

检查is_numeric($var)http//php.net/is_numeric

试试这个

if((int)$variable) {...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM