How do you check if a number is int or float in php?

Artefacto's answer will also convert a 5.0 float number into a 5 integer. If that is bugging you like it was bugging me, I offer the answer of automatic type conversion via the multiplication operator:

If either operand is a float, then both operands are evaluated as floats, and the result will be a float. Otherwise, the operands will be interpreted as ints, and the result will also be an int.

$demo = array[
    1, 2, 3, '1', '2', '3', 
    1.0, 2.1, 3.2, '1.0', '2.1', '3.2', 
    -17, '-17', 
    -1.0, -2.2, '-1.0', '-2.2',
    0, '0', -0, '-0',
    0.0, '0.0', -0.0, '-0.0',
];

foreach [$demo as $k => $v] {
    if [is_numeric[$v] && is_string[$v]] {
        $demo[$k] = 1 * $v;
    }
}

echo "
"; var_dump[$demo]; echo "
"; array[26] { [0]=> int[1] [1]=> int[2] [2]=> int[3] [3]=> int[1] [4]=> int[2] [5]=> int[3] [6]=> float[1] [7]=> float[2.1] [8]=> float[3.2] [9]=> float[1] [10]=> float[2.1] [11]=> float[3.2] [12]=> int[-17] [13]=> int[-17] [14]=> float[-1] [15]=> float[-2.2] [16]=> float[-1] [17]=> float[-2.2] [18]=> int[0] [19]=> int[0] [20]=> int[0] [21]=> int[0] [22]=> float[0] [23]=> float[0] [24]=> float[-0] [25]=> float[-0] }

[PHP 4, PHP 5, PHP 7, PHP 8]

is_intFind whether the type of a variable is integer

Description

is_int[mixed $value]: bool

Note:

To test if a variable is a number or a numeric string [such as form input, which is always a string], you must use is_numeric[].

Parameters

value

The variable being evaluated.

Return Values

Returns true if value is an int, false otherwise.

Examples

Example #1 is_int[] example

The above example will output:

is_int[23] = bool[true]
is_int['23'] = bool[false]
is_int[23.5] = bool[false]
is_int['23.5'] = bool[false]
is_int[NULL] = bool[false]
is_int[true] = bool[false]
is_int[false] = bool[false]

See Also

  • is_bool[] - Finds out whether a variable is a boolean
  • is_float[] - Finds whether the type of a variable is float
  • is_numeric[] - Finds whether a variable is a number or a numeric string
  • is_string[] - Find whether the type of a variable is string
  • is_array[] - Finds whether a variable is an array
  • is_object[] - Finds whether a variable is an object

Simon Neaves

14 years ago

I've found that both that is_int and ctype_digit don't behave quite as I'd expect, so I made a simple function called isInteger which does. I hope somebody finds it useful.

Robin

12 years ago

Keep in mind that is_int[] operates in signed fashion, not unsigned, and is limited to the word size of the environment php is running in.

In a 32-bit environment:



In a 64-bit environment:



If you find yourself deployed in a 32-bit environment where you are required to deal with numeric confirmation of integers [and integers only] potentially breaching the 32-bit span, you can combine is_int[] with is_float[] to guarantee a cover of the full, signed 64-bit span:

e dot sand at elisand dot com

13 years ago

Simon Neaves was close on explaining why his function is perfect choice for testing for an int [as possibly most people would need].  He made some errors on his ctype_digit[] output though - possibly a typo, or maybe a bug in his version of PHP at the time.

The correct output for parts of his examples should be:



As you can see, the reason why using *just* ctype_digit[] may not always work is because it only returns TRUE when given a string as input - given a number value and it returns FALSE [which may be unexpected].

andre dot roesti at 7flex dot net

12 years ago

With this function you can check if every of multiple variables are int. This is a little more comfortable than writing 'is_int' for every variable you've got.

davide dot renzi at gmail dot com

6 years ago

I've found a faster way of determining an integer.
On my env, this method takes about half the time of using is_int[].

Cast the value then check if it is identical to the original.

petepostma at gmail dot spam dot com

10 years ago

There is a versa to the vice of this int only type check.

is_int[ $integer_type] will only return true, if the TYPE is int, not the value
ctype_digit[ $string_type] will only return true if the TYPE is string, and its value is INT

therefore:
return [ is_int[$value] || ctype_digit[$value] ];

nicolas dot giraud at actiane dot com

10 years ago

Just a shorter way to check if your variable is an int or a string containing a int without others digit than 0 to 9 :

Chủ Đề