Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a variable is an integer in PHP

Tags:

php

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

like image 882
ahota Avatar asked Jun 20 '11 20:06

ahota


People also ask

How do you know if a variable is an integer?

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

How do you check if a string is a number PHP?

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.

Is numeric or int PHP?

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.

Is 0 an int PHP?

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


1 Answers

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 ]; 

The first option (FILTER_VALIDATE_INT way) :

# 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 ✔ 

The second option (CASTING COMPARISON way) :

# 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 ✔ 

The third option (CTYPE_DIGIT way) :

# 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 ✔ 

The fourth option (REGEX way) :

# 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 ✔ 
like image 170
Cam CHN Avatar answered Oct 01 '22 03:10

Cam CHN