Skip to content
PHP Logo

How can you use the is_numeric function in PHP to validate user input?

When validating user input in PHP, you can use the is_numeric function to check if the input is a numerical value. This function returns true if the input is a number or a numeric string, and false otherwise. By using this function, you can ensure that the user input is a valid numeric value before further processing it in your application.

php
$user_input = $_POST['user_input'];

if (is_numeric($user_input)) {
    // Proceed with further processing of the numeric input
    echo "Input is a valid number: " . $user_input;
} else {
    // Handle the case where the input is not a valid number
    echo "Invalid input. Please enter a numeric value.";
}