I have the following code
    $page = $_GET['p'];      if($page == "")     {         $page = 1;     }     if(is_int($page) == false)     {         setcookie("error", "Invalid page.", time()+3600);         header("location:somethingwentwrong.php");         die();     }     //else continue with code   which I am going to use for looking at different "pages" of a database (results 1-10, 11-20, etc). I can't seem to get the is_int() function to work correctly, however. Putting "1" into the url (noobs.php?p=1) gives me the invalid page error, as well as something like "asdf".
To check if the variable is an integer in Python, we will use isinstance() which will return a boolean value whether a variable is of type integer or not. After writing the above code (python check if the variable is an integer), Ones you will print ” isinstance() “ then the output will appear as a “ True ”.
To check if given string is a number or not, use PHP built-in function is_numeric(). is_numeric() takes string as an argument and returns true if the string is all numbers, else it returns false.
The key difference between these two functions is that is_int() checks the type of variable, while is_numeric() checks the value of the variable. $var = "123"; $var is a string of numbers, not an integer value.
PHP converts the string to an int, which is 0 (as it doesn't contain any number representation).
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.
To do this correctly, you can use one of these options:
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 ];   # Check if your variable is an integer if ( filter_var($variable, FILTER_VALIDATE_INT) === false ) {   echo "Your variable is not an integer"; }   Output :
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 ✔   # Check if your variable is an integer if ( strval($variable) !== strval(intval($variable)) ) {   echo "Your variable is not an integer"; }   Output :
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 ✔   # Check if your variable is an integer if ( ! ctype_digit(strval($variable)) ) {   echo "Your variable is not an integer"; }   Output :
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 ✔   # Check if your variable is an integer if ( ! preg_match('/^\d+$/', $variable) ) {   echo "Your variable is not an integer"; }   Output :
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 ✔ 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With