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

Questions:

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"

etc.
In python, I could simply to the following:

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

How can I achive this in PHP?

How to&Answers:

Use 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)

Answer:

Integer (not just numeric) test:
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
Fails with integer-valued float type: (double)123

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

Answer:

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.

Answer:

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

Answer:

try this

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