Home » Php » int – PHP check if object can be converted to integer?

int – PHP check if object can be converted to integer?

Posted by: admin July 12, 2020 Leave a comment


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. 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.

I.e., valid objects would be

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

And not valid would be

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

In python, I could simply to the following:

except (TypeError, ValueError):
    return False
return True

How can I achive this in PHP?

How to&Answers:

Use is_numeric.

$tests = array(
    "not numeric", 

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:

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;


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
Fails with integer-valued float type: (double)123

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


See PHP’s ctype_digit().

This function evaluates a string to see if all character are numeric. Thus "1.1" will not return true because "." is not numeric, but "11" will. Also note this works for strings only, so numbers without the surrounding quotation marks will also not work.


check out is_numeric($var): http://php.net/is_numeric


try this

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