Checking if a variable is an integer in PHP

Checking if a variable is an integer in PHP

  1. Blog
  2. 15 November 2026
  3. 1 min read

Stefan Bogdanescu

Founder & Senior Architect

Checking if a variable is an integer in PHP

You are encountering a common pitfall when validating query parameters in PHP: $_GET variables are always treated as strings, even if they contain digits. When you pass "1" via a URL like noobs.php?p=1, the $page variable holds the string "1", not the integer 1. Consequently, the comparison $page == "" fails because a string is never identical to an empty string unless explicitly coerced, but more importantly, is_int("1") returns false. This causes your validation logic to incorrectly flag valid numeric input as invalid.

To fix this, you must explicitly cast the variable or use a filter that handles the coercion for you. The most robust approach for pagination parameters is to use filter_var() with the FILTER_VALIDATE_INT flag. This function attempts to parse the string and returns either the integer value if successful or false if it fails.

$page = $_GET['p'] ?? 1; // Default to 1 if key is missing

// Filter allows for strict integer validation (no floats, no strings)
if (!filter_var($page, FILTER_VALIDATE_INT)) {
    setcookie("error", "Invalid page.", time() + 3600);
    header("Location: somethingwentwrong.php");
    exit();
}

// Optional: Enforce a minimum limit to prevent negative indices or zero
if ($page < 1) {
    $page = 1;
}

Using filter_var is superior to manual casting ((int)$page) in validation contexts because it prevents float values like 1.5 from passing through, which could cause off-by-one errors when calculating database offsets. If you cast a float string to an integer immediately after validation, you might silently accept invalid input depending on how strict your subsequent logic is.

Another critical consideration is the handling of non-numeric characters or leading whitespace. While filter_var is robust, it does not automatically strip whitespace from the string before attempting conversion in older PHP versions, though modern versions often handle this gracefully. However, relying solely on is_int() without checking the raw value first is insufficient because is_int() will always return false for strings derived from $_GET.

If you prefer to stick with is_int(), you must cast the variable before passing it to the function:

$page = (int)($_GET['p'] ?? 1);

if (!is_int($page)) {
    // Handle error
}

This approach assumes that if the input is not an integer, casting it will result in 0 or a warning depending on context, but checking is_int() after casting confirms the result is strictly an integer. However, note that (int)"1" results in 1, so is_int((int)$page) would technically pass for any valid numeric string, including those representing floats truncated to integers (e.g., "1.9" becomes 1). If your pagination logic requires exact matches and rejects decimals like 1.5 entirely, the filter_var approach is safer as it returns false immediately upon seeing a decimal point.

Additionally, you should validate the upper bound of the page number. Even if an integer passes validation, allowing extremely large values can lead to performance issues or memory exhaustion when executing SQL queries like LIMIT 1000000, 10. Always clamp the value:

$maxPages = 100; // Reasonable limit for your application
if ($page > $maxPages) {
    setcookie("error", "Page number too high.", time() + 3600);
    header("Location: somethingwentwrong.php");
    exit();
}

For developers working within the Laravel ecosystem, this kind of manual validation is often abstracted away using Request components. You can define rules in your controller or form requests to handle integer validation, ensuring type safety throughout the application lifecycle without writing repetitive if statements for every parameter.

Our network