Strict Integer Validation in PHP
Validating user input often requires distinguishing between integers and floating-point numbers, even when both are represented as strings. The standard is_numeric() function is too permissive; it returns true for "2" but also for "2.0" or "2.1". Conversely, is_int() checks the internal data type of a variable, which means it will return false if you pass it a string like "2", even though that string represents a valid integer value.
To solve this, you need a custom validation logic that strips non-integer characters before checking the result. The most reliable approach is to use filter_var() with the FILTER_VALIDATE_INT flag. This function attempts to parse the string as an integer and returns false if the input contains any fractional parts or invalid characters.
function my_is_int(string $string): bool {
return filter_var($string, FILTER_VALIDATE_INT) !== false;
}
When you pass "2" to this function, filter_var() successfully parses it and returns the integer value 2. Since 2 is not false, the expression evaluates to true. However, when you pass "2.1", the parser fails because a decimal point exists where an integer expects digits only. The function returns false, which satisfies your requirement to reject doubles.
Handling Negative Numbers and Edge Cases
It is important to understand how this validation behaves with negative integers and boundary values. The FILTER_VALIDATE_INT flag accepts standard integer notation, including a leading minus sign.
my_is_int("-5") // true
my_is_int("0") // true
my_is_int("+10") // false in some PHP versions depending on strictness, often treated as invalid without explicit handling
In many contexts, a leading + sign is not expected for simple integer inputs. Depending on the PHP version and configuration, filter_var() might reject " +10" or treat it strictly. If you need to support signed integers explicitly, ensure your validation logic accounts for that specific requirement. However, for typical form inputs where users type plain numbers, rejecting the + sign is usually the safer default to prevent ambiguity.
Why Not Cast First?
A common temptation is to simply cast the string to an integer using (int)$string and then check if it equals itself or use a regex. While casting "2.1" to an integer results in 2, this approach silently truncates the fractional part rather than rejecting the input. You lose the ability to tell the user that they entered a decimal number when you only needed an integer. By relying on filter_var(), you maintain the distinction between "valid integer" and "invalid format," allowing you to provide a helpful error message like "Please enter a whole number only."
Performance Considerations
For high-traffic applications, function call overhead can matter. The filter_var() approach is slightly more expensive than a raw is_numeric() check because it involves internal parsing logic specific to the filter type. However, this cost is negligible compared to database queries or external API calls. If you are validating thousands of inputs per second in a tight loop without database interaction, you might consider a regular expression for marginal speed gains, but filter_var() remains the most robust and readable solution.
// Regex alternative (less readable, more brittle regarding locale)
function my_is_int_regex(string $string): bool {
return preg_match('/^-?\d+$/', $string) === 1;
}
The regex approach is faster but requires careful handling of negative numbers and potential locale-specific digit variations. Unless you have proven performance bottlenecks, the readability and built-in safety of filter_var() outweigh the micro-optimizations provided by a raw regex.
For developers working within the Laravel ecosystem, this validation pattern fits seamlessly into form request classes. You can define custom validation rules or use the is_int method if available in your version, but understanding the underlying mechanics ensures you handle edge cases correctly when building custom validators.
