HOWTO · PHP
How to Check for Numerical Characters in PHP
Learn how PHP digit, pattern, numeric-string, and integer checks differ, and choose the rule that matches your input.
On this page
PHP has no single check that answers every question about numeric input. A digit-only test rejects signs and decimal points; a custom pattern can allow a chosen format; is_numeric follows PHP’s numeric-string rules; and an integer filter checks for an integer representation. Choose based on what your application accepts, not on whether a value merely looks like a number.
The examples use strings because form and request values usually arrive as text. The PHP Manual documents ctype_digit, preg_match, is_numeric, strpos, and filter_var. The supplied runtime evidence records successful execution of all four examples below.
Choose the PHP numeric check that matches your input
Use ctype_digit when the entire nonempty string must contain decimal digits only. Use preg_match when the application has a specific text format, such as an optional sign and decimal point. Use is_numeric when PHP’s numeric-string syntax is acceptable, including exponent notation. Use filter_var with FILTER_VALIDATE_INT when the input must be an integer. These checks are not interchangeable: 3.5 is numeric but not an integer, while 53e10 is a valid numeric string but is not a digit-only string.
Use ctype_digit for strings of digits
ctype_digit returns true only when a string contains one or more decimal digits and every character is a digit. It does not accept a sign, decimal point, whitespace, or exponent marker. Pass the variable itself; quoting its name would test the literal characters in the name instead of the input value.
Run this example:
<?php
$digits = '200';
echo ctype_digit($digits) ? 'true' : 'false';
For example, 200 fits this rule, while 53e10 does not. If your value may be an integer rather than a string, normalize and validate its type according to the rest of your application before relying on a character check.
Use preg_match for a custom numeric format
The pattern below accepts an optional minus sign, optional digits before a decimal point, and at least one digit overall; it therefore accepts forms such as 200, -12, .5, and -3.5. It rejects 2x3 and exponent forms such as 53e10. It allows leading zeros and does not accept a leading plus sign. Change the pattern deliberately if those rules do not match your input policy. preg_match returns 1 for a match, 0 for no match, and false if matching fails.
<?php
$pattern = '/\A-?[0-9]*\.?[0-9]+\z/';
foreach (['200', '3.5', '2x3', '53e10'] as $value) {
echo $value . ': ' . (preg_match($pattern, $value) === 1 ? 'match' : 'no match') . PHP_EOL;
}
Use is_numeric when PHP numeric strings are valid
is_numeric accepts numeric strings understood by PHP, including exponent notation in both 53e10 and 53E10. If your application accepts ordinary numeric strings but forbids exponent notation, add that policy check explicitly. strpos returns position zero when a character occurs at the start, so compare its result strictly with false rather than treating it as a boolean.
<?php
foreach (['53e10', '53E10', '53.10', '2x3'] as $value) {
$has_exponent = strpos($value, 'e') !== false || strpos($value, 'E') !== false;
$accepted = is_numeric($value) && !$has_exponent;
echo $value . ': ' . ($accepted ? 'accepted' : 'rejected') . PHP_EOL;
}
This combines two separate decisions: PHP must recognize the string as numeric, and the application must not find either exponent letter. If exponent notation is valid for your use case, call is_numeric without the extra restriction.
Validate integer input with filter_var
Pass FILTER_VALIDATE_INT as the filter to filter_var. Compare the result with false using ===, because a valid integer value of zero is falsy in a loose condition. A failed integer validation does not prove that the text contains nonnumeric characters: 3.5 is numeric but not an integer representation, and other rejected values may violate the filter’s accepted format or range.
<?php
foreach (['53e10', '3.5', '2x3', '200', '0'] as $value) {
$result = filter_var($value, FILTER_VALIDATE_INT);
echo $value . ': ' . ($result === false ? 'not an integer' : 'integer') . PHP_EOL;
}
Choose the narrowest rule that expresses your input contract. For a custom decimal format, use a pattern; for PHP’s numeric-string syntax, use is_numeric; for whole-number text, use FILTER_VALIDATE_INT; and for unsigned digit strings, use ctype_digit.