Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 709
Niklas R Avatar asked Mar 05 '12 11:03

Niklas R


People also ask

How do you check if a value is an integer in PHP?

The is_int() function checks whether a variable is of type integer or not. This function returns true (1) if the variable is of type integer, otherwise it returns false.

How check if string is integer PHP?

The is_numeric() function checks whether a variable is a number or a numeric string. This function returns true (1) if the variable is a number or a numeric string, otherwise it returns false/nothing.

How do I echo an integer in PHP?

There are a number of ways to "convert" an integer to a string in PHP. The traditional computer science way would be to cast the variable as a string: $int = 5; $int_as_string = (string) $int; echo $int . ' is a '.

Is 0 an int PHP?

PHP converts the string to an int, which is 0 (as it doesn't contain any number representation).


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

like image 140
Dogbert Avatar answered Oct 21 '22 07:10

Dogbert


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);
}
like image 6
biziclop Avatar answered Oct 21 '22 07:10

biziclop


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.

like image 3
Will Squire Avatar answered Oct 21 '22 06:10

Will Squire