Skip to content
PHP Logo

What are common methods for validating numerical input in PHP?

When working with numerical input in PHP, it's important to validate the data to ensure it meets the expected criteria. Common methods for validating numerical input include checking if the input is numeric, within a specific range, or not empty.

php
// Validate if input is numeric
$input = $_POST['number'];
if (!is_numeric($input)) {
    echo "Input must be a number.";
}

// Validate if input is within a specific range
$min = 1;
$max = 100;
if ($input < $min || $input > $max) {
    echo "Input must be between $min and $max.";
}

// Validate if input is not empty
if (empty($input)) {
    echo "Input cannot be empty.";
}