Home >Backend Development >PHP Tutorial >How to Reliably Check if a Variable Represents an Integer in PHP?
When working with user input, it's crucial to validate the data to ensure its integrity. One common validation task is checking if a variable represents an integer. PHP provides the is_int() function for this purpose, but it may not always produce the desired results.
Using is_int() may return false positives for values that appear to be integers but aren't recognized as such by the function. For example, consider the case where a user enters "1" as a page number:
<code class="php">$page = $_GET['p'];</code>
In this case, $page will be assigned the string "1," which will result in is_int($page) returning false.
To reliably check if a variable is an integer, consider using alternative methods that provide more accurate results:
The FILTER_VALIDATE_INT filter can be used to validate integer inputs:
<code class="php">if (filter_var($page, FILTER_VALIDATE_INT) === false) { // Not an integer }</code>
Output:
TEST -1: -1 (type:integer) Your variable is an integer ✔ 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 ✔
Comparing the string representation of a variable with its integer representation can also reveal whether it's an integer:
<code class="php">if (strval($page) !== strval(intval($page))) { // Not an integer }</code>
Output:
TEST -1: -1 (type:integer) Your variable is an integer ✔ 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 ✔
Using the ctype_digit function, you can check for positive numbers and 0:
<code class="php">if (!ctype_digit(strval($page))) { // Not an integer }</code>
Output:
TEST -1: -1 (type:integer) # Your variable is not an integer ✘ 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 ✔
Regular expressions can also be used to validate integers:
<code class="php">if (!preg_match('/^-?\d+$/', $page)) { // Not an integer }</code>
Output:
TEST -1: -1 (type:integer) Your variable is an integer ✔ 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 above is the detailed content of How to Reliably Check if a Variable Represents an Integer in PHP?. For more information, please follow other related articles on the PHP Chinese website!