Is_numeric () vs. is_float () vs. is_int ()

My understanding ...

if is_numeric($input) === true

either

is_float($input) === true OR

is_int($input) === true OR

$input === 0 OR

$input is a numeric string (this means that it will satisfy one of the first 3 if it was not enclosed in quotation marks).

That's for sure? Are there other differences?

+8
types php numbers
source share
2 answers

See the PHP documentation on is_numeric . It talks about everything that is allowed, and it is more than is_float and is_int .


It is also important to note that is_int only works with things of type integer, which means that string representations are not allowed. This is a common problem when verifying that form input is an integer. You must use filter_var or something from the filter family with the filter FILTER_VALIDATE_INT . For floats, use FILTER_VALIDATE_FLOAT .


Also, if the reason you are trying to check for an integer is to check the parameter as an int, then in PHP 7 you can do this:

 function foo(int $i) { // $i is guaranteed to be an int (is_int) will be true } 

PHP 7 has two different conversion modes to int; this answer explains it a little more.

Note that this is probably not what you want if you are checking the contents of a form element. Use the filter_var solution for this.

+24
source share

See docs . The numerical value may be:

  • Integer
  • Float
  • Exponential
  • Positive hex
  • A string containing most of these
+2
source share

All Articles