php check to see if variable is integer

php check to see if variable is integer

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

Stefan Bogdanescu

Founder & Senior Architect

Checking for Integer Values in PHP Without Casting Pitfalls

Determining whether a variable holds an integer value in PHP is a common task, but the behavior of type-checking functions often leads to unexpected results. Many developers encounter issues when mixing strings that represent integers with actual integer types, or when floating-point numbers slip into the mix. The goal is to distinguish between valid integers (like 23 or the string '23') and invalid ones (like 22.3 or '23.3').

The standard function is_int() is the most direct approach, but it has a strict limitation: it returns false for numeric strings. If your input comes from an HTML form where you have not yet processed the data, it will arrive as a string by default. Consequently, checking is_int($_POST['id']) will fail even if the user entered "23".

To handle this, developers often reach for (integer) casting or type juggling. However, relying on implicit conversion logic can introduce subtle bugs. Consider the following approach:

if ($_POST['id'] != (integer)$_POST['id']) {
    echo 'not an integer';
}

This logic attempts to convert the value to an integer and compare it against the original. If the original is a string like '23', casting it yields 23. The comparison 23 == 23 evaluates to true, so the condition inside the if statement is false, and the error message is not printed. This seems correct at first glance. However, this pattern breaks down when the variable is a string that cannot be cleanly converted, or if the original value is already an integer but the cast behaves differently in edge cases involving type safety.

A more robust solution involves combining is_numeric() with a strict type check on the numeric representation. The strategy is to first ensure the input contains only digits (excluding decimals), then verify that it can be treated as an integer. Here is a pattern that aligns with your specific requirements:

$rawValue = $_POST['id'] ?? null;

// Check if the value is strictly numeric and does not contain a decimal point
if ($rawValue === '' || !is_numeric($rawValue) || strpos($rawValue, '.') !== false) {
    echo 'not an integer';
} else {
    // Ensure that when cast to int, it matches the original string representation
    if ((string)(int)$rawValue !== (string)$rawValue) {
        echo 'not an integer';
    }
}

This logic handles several scenarios effectively:

  • 23: is_numeric() returns true, no decimal point exists. Casting to int results in 23, which matches the string '23'. Result: Valid.
  • '23': Same as above. The cast preserves the value. Result: Valid.
  • 22.3: is_numeric() returns true, but strpos($rawValue, '.') finds a decimal point. Result: Invalid.
  • '23.3': Similarly, the presence of a dot triggers the rejection logic immediately.

The use of strpos to detect the decimal separator is a reliable way to filter out floats before attempting stricter integer validation. This avoids the need for complex arithmetic comparisons or relying solely on the behavior of (integer) casting, which can sometimes obscure floating-point inaccuracies depending on the PHP version and architecture.

If you are working within a Laravel application, this kind of input validation is critical early in the request lifecycle. While Laravel provides form request classes to handle this elegantly for API endpoints, server-side scripts or older MVC architectures often require these manual checks. Always remember that $_POST data is always treated as a string until explicitly cast or validated, so assuming it is an integer without checking leads to logic errors.

By explicitly rejecting strings containing decimal points and verifying the numeric nature of the input, you ensure that your application only accepts genuine integer values, regardless of whether they arrive as numbers or digit-only strings.

Our network