- Checking if a variable is an integer in PHP
- The first option (FILTER_VALIDATE_INT way) :
- The second option (CASTING COMPARISON way) :
- The third option (CTYPE_DIGIT way) :
- The fourth option (REGEX way) :
- Answer by Cal Townsend
- Answer by Zaylee Wagner
- The first option (FILTER_VALIDATE_INT way) :
- The second option (CASTING COMPARISON way) :
- The third option (CTYPE_DIGIT way) :
- The fourth option (REGEX way) :
- Answer by Kynlee Ortega
- Answer by Alianna Hunt
- is_int
- Parameters
- Return Values
- Examples
- See Also
- User Contributed Notes 30 notes
Checking if a variable is an integer in PHP
Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Check out my question here: How to check whether a variable in $_GET Array is an integer?,I think this is easiest way to check if our var is int, string, double or Boolean.,Using is_numeric() for checking if a variable is an integer is a bad idea. This function will return TRUE for 3.14 for example. It’s not the expected behavior.
Considering this variables array :
$variables = [ "TEST 0" => 0, "TEST 1" => 42, "TEST 2" => 4.2, "TEST 3" => .42, "TEST 4" => 42., "TEST 5" => "42", "TEST 6" => "a42", "TEST 7" => "42a", "TEST 8" => 0x24, "TEST 9" => 1337e0 ];
The first option (FILTER_VALIDATE_INT way) :
# Check if your variable is an integer if ( filter_var($variable, FILTER_VALIDATE_INT) === false )
TEST 0 : 0 (type:integer) is an integer ✔ TEST 1 : 42 (type:integer) is an integer ✔ TEST 2 : 4.2 (type:double) is not an integer ✘ TEST 3 : 0.42 (type:double) is not an integer ✘ TEST 4 : 42 (type:double) is an integer ✔ TEST 5 : 42 (type:string) is an integer ✔ TEST 6 : a42 (type:string) is not an integer ✘ TEST 7 : 42a (type:string) is not an integer ✘ TEST 8 : 36 (type:integer) is an integer ✔ TEST 9 : 1337 (type:double) is an integer ✔
The second option (CASTING COMPARISON way) :
# Check if your variable is an integer if ( strval($variable) !== strval(intval($variable)) )
TEST 0 : 0 (type:integer) is an integer ✔ TEST 1 : 42 (type:integer) is an integer ✔ TEST 2 : 4.2 (type:double) is not an integer ✘ TEST 3 : 0.42 (type:double) is not an integer ✘ TEST 4 : 42 (type:double) is an integer ✔ TEST 5 : 42 (type:string) is an integer ✔ TEST 6 : a42 (type:string) is not an integer ✘ TEST 7 : 42a (type:string) is not an integer ✘ TEST 8 : 36 (type:integer) is an integer ✔ TEST 9 : 1337 (type:double) is an integer ✔
The third option (CTYPE_DIGIT way) :
# Check if your variable is an integer if ( ! ctype_digit(strval($variable)) )
TEST 0 : 0 (type:integer) is an integer ✔ TEST 1 : 42 (type:integer) is an integer ✔ TEST 2 : 4.2 (type:double) is not an integer ✘ TEST 3 : 0.42 (type:double) is not an integer ✘ TEST 4 : 42 (type:double) is an integer ✔ TEST 5 : 42 (type:string) is an integer ✔ TEST 6 : a42 (type:string) is not an integer ✘ TEST 7 : 42a (type:string) is not an integer ✘ TEST 8 : 36 (type:integer) is an integer ✔ TEST 9 : 1337 (type:double) is an integer ✔
The fourth option (REGEX way) :
# Check if your variable is an integer if ( ! preg_match('/^\d+$/', $variable) )
TEST 0 : 0 (type:integer) is an integer ✔ TEST 1 : 42 (type:integer) is an integer ✔ TEST 2 : 4.2 (type:double) is not an integer ✘ TEST 3 : 0.42 (type:double) is not an integer ✘ TEST 4 : 42 (type:double) is an integer ✔ TEST 5 : 42 (type:string) is an integer ✔ TEST 6 : a42 (type:string) is not an integer ✘ TEST 7 : 42a (type:string) is not an integer ✘ TEST 8 : 36 (type:integer) is an integer ✔ TEST 9 : 1337 (type:double) is an integer ✔
Answer by Cal Townsend
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(). ,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_bool() — Finds out whether a variable is a boolean
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)
Answer by Zaylee Wagner
Using is_numeric() for checking if a variable is an integer is a bad idea. This function will return TRUE for 3.14 for example. It’s not the expected behavior.,Instead try is_numeric() or ctype_digit(),Considering this variables array : ,When the browser sends p in the querystring, it is received as a string, not an int. is_int() will therefore always return false.
$variables = [ "TEST 0" => 0, "TEST 1" => 42, "TEST 2" => 4.2, "TEST 3" => .42, "TEST 4" => 42., "TEST 5" => "42", "TEST 6" => "a42", "TEST 7" => "42a", "TEST 8" => 0x24, "TEST 9" => 1337e0 ];
The first option (FILTER_VALIDATE_INT way) :
# Check if your variable is an integer if ( filter_var($variable, FILTER_VALIDATE_INT) === false )
TEST 0 : 0 (type:integer) is an integer ✔ TEST 1 : 42 (type:integer) is an integer ✔ TEST 2 : 4.2 (type:double) is not an integer ✘ TEST 3 : 0.42 (type:double) is not an integer ✘ TEST 4 : 42 (type:double) is an integer ✔ TEST 5 : 42 (type:string) is an integer ✔ TEST 6 : a42 (type:string) is not an integer ✘ TEST 7 : 42a (type:string) is not an integer ✘ TEST 8 : 36 (type:integer) is an integer ✔ TEST 9 : 1337 (type:double) is an integer ✔
The second option (CASTING COMPARISON way) :
# Check if your variable is an integer if ( strval($variable) !== strval(intval($variable)) )
TEST 0 : 0 (type:integer) is an integer ✔ TEST 1 : 42 (type:integer) is an integer ✔ TEST 2 : 4.2 (type:double) is not an integer ✘ TEST 3 : 0.42 (type:double) is not an integer ✘ TEST 4 : 42 (type:double) is an integer ✔ TEST 5 : 42 (type:string) is an integer ✔ TEST 6 : a42 (type:string) is not an integer ✘ TEST 7 : 42a (type:string) is not an integer ✘ TEST 8 : 36 (type:integer) is an integer ✔ TEST 9 : 1337 (type:double) is an integer ✔
The third option (CTYPE_DIGIT way) :
# Check if your variable is an integer if ( ! ctype_digit(strval($variable)) )
TEST 0 : 0 (type:integer) is an integer ✔ TEST 1 : 42 (type:integer) is an integer ✔ TEST 2 : 4.2 (type:double) is not an integer ✘ TEST 3 : 0.42 (type:double) is not an integer ✘ TEST 4 : 42 (type:double) is an integer ✔ TEST 5 : 42 (type:string) is an integer ✔ TEST 6 : a42 (type:string) is not an integer ✘ TEST 7 : 42a (type:string) is not an integer ✘ TEST 8 : 36 (type:integer) is an integer ✔ TEST 9 : 1337 (type:double) is an integer ✔
The fourth option (REGEX way) :
# Check if your variable is an integer if ( ! preg_match('/^\d+$/', $variable) )
TEST 0 : 0 (type:integer) is an integer ✔ TEST 1 : 42 (type:integer) is an integer ✔ TEST 2 : 4.2 (type:double) is not an integer ✘ TEST 3 : 0.42 (type:double) is not an integer ✘ TEST 4 : 42 (type:double) is an integer ✔ TEST 5 : 42 (type:string) is an integer ✔ TEST 6 : a42 (type:string) is not an integer ✘ TEST 7 : 42a (type:string) is not an integer ✘ TEST 8 : 36 (type:integer) is an integer ✔ TEST 9 : 1337 (type:double) is an integer ✔
You can see this by calling var_dump :
var_dump($_GET['p']); // string(2) "54"
Answer by Kynlee Ortega
The is_int () function is used to test whether the type of the specified variable is an integer or not. ,*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.,TRUE if var_name is an integer, FALSE otherwise.,Note that you don’t need to calculate a difference if you are spawning a php instance for every test.
" ; > else < echo "$var_name1 is not an Integer
" ; > if (is_int($var_name2)) < echo "$var_name2 is Integer
" ; > else < echo "$var_name2 is not Integer
" ; > $result=is_int($var_name3); echo "[ $var_name3 is Integer? ]" .var_dump($result)."
"; $result=is_int($var_name4); echo "[ $var_name4 is Integer? ]" .var_dump($result)."
"; $result=is_int($var_name5); echo "[ $var_name5 is Integer? ]" .var_dump($result)."
"; $result=is_int($var_name6); echo "[ $var_name6 is Integer? ]" .var_dump($result)."
"; $result=is_int($var_name7); echo "[ $var_name7 is Integer? ]" .var_dump($result); ?>
678 is Integer a678 is not Integer bool(false) [ 678 is Integer? ] bool(true) [ 999 is Integer? ] bool(false) [ 698.99 is Integer? ] bool(false) [ Array is Integer? ] bool(false) [ 125689.66 is Integer? ]
Answer by Alianna Hunt
// to check the input integer validation we can use is_int() function Syntax: is_int(parameter); $x = 10; //returns true $x = "123"; //returns false $x = 12.365; //returns false $x = "ankur"; //returns false is_int($x);
is_int
Finds whether the type of the given variable is integer.
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
The variable being evaluated.
Return Values
Returns true if value is an int , false otherwise.
Examples
Example #1 is_int() example
$values = array( 23 , «23» , 23.5 , «23.5» , null , true , false );
foreach ( $values as $value ) echo «is_int(» ;
var_export ( $value );
echo «) color: #007700″>;
var_dump ( is_int ( $value ));
>
?>?php
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
User Contributed Notes 30 notes
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.
function isInteger ( $input ) return( ctype_digit ( strval ( $input )));
>
var_dump ( is_int ( 23 )); //bool(true)
var_dump ( is_int ( «23» )); //bool(false)
var_dump ( is_int ( 23.5 )); //bool(false)
var_dump ( is_int ( NULL )); //bool(false)
var_dump ( is_int ( «» )); //bool(false)
var_dump ( ctype_digit ( 23 )); //bool(true)
var_dump ( ctype_digit ( «23» )); //bool(false)
var_dump ( ctype_digit ( 23.5 )); //bool(false)
var_dump ( ctype_digit ( NULL )); //bool(false)
var_dump ( ctype_digit ( «» )); //bool(true)
var_dump ( isInteger ( 23 )); //bool(true)
var_dump ( isInteger ( «23» )); //bool(true)
var_dump ( isInteger ( 23.5 )); //bool(false)
var_dump ( isInteger ( NULL )); //bool(false)
var_dump ( isInteger ( «» )); //bool(false)
?>
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.
is_int ( 2147483647 ); // true
is_int ( 2147483648 ); // false
is_int ( 9223372036854775807 ); // false
is_int ( 9223372036854775808 ); // false
?>
In a 64-bit environment:
is_int ( 2147483647 ); // true
is_int ( 2147483648 ); // true
is_int ( 9223372036854775807 ); // true
is_int ( 9223372036854775808 ); // false
?>
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:
$small = 2147483647 ; // will always be true for is_int(), but never for is_float()
$big = 9223372036854775807 ; // will only be true for is_int() in a 64-bit environment
if( is_int ( $small ) || is_float ( $small ) ); // passes in a 32-bit environment
if( is_int ( $big ) || is_float ( $big ) ); // passes in a 32-bit environment
?>
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:
var_dump ( ctype_digit ( 23 )); //bool(false)
var_dump ( ctype_digit ( «23» )); //bool(true)
var_dump ( ctype_digit ( 23.5 )); //bool(false)
var_dump ( ctype_digit ( NULL )); //bool(false)
var_dump ( ctype_digit ( «» )); //bool(false)
?>
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).
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.
function are_int ( ) $args = func_get_args ();
foreach ( $args as $arg )
if ( ! is_int ( $arg ) )
return false ;
return true ;
>
// Example:
are_int ( 4 , 9 ); // true
are_int ( 22 , 08, ‘foo’ ); // false
?>