is_numeric() accepts integers, floats and numeric strings, including supported scientific notation. Use it when numeric strings are acceptable; use integer validation when only whole numbers are valid.
$value = $_POST['amount'] ?? '';
if (is_numeric($value)) {
$amount = (float) $value;
echo 'Numeric value: ' . $amount;
} else {
echo 'Enter a number.';
}
This is the recommended starting pattern. The detailed examples below are retained for explanation, comparison and related variations.
is_numeric(number);
The above checking will return true or false based on the value stored on number. Let us try some examples. The output of each type of checking is given at its right side within comments.
echo is_numeric(12.35);// Output 1
echo "<br>";
echo is_numeric(.35);// Output 1
echo "<br>";
echo is_numeric(10e9);// Output 1
echo "<br>";
echo is_numeric(+11e-9)? "T" :"F";// Output T
echo "<br>";
echo is_numeric("05.050.1") ? "T" : "F";// Output F
echo "<br>";
$val=13.58;
echo is_numeric($val); // Output 1
We can also use FILTER_VALIDATE_INT php filter to validate integer.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.