How can regular expressions be effectively used to validate numerical input in PHP?

Regular expressions can be effectively used to validate numerical input in PHP by defining a pattern that only allows numeric characters. This can help ensure that the input contains only numbers and no other characters, making it easier to validate and process the data accurately.

$input = "12345";
$pattern = '/^\d+$/';

if (preg_match($pattern, $input)) {
    echo "Input is a valid number.";
} else {
    echo "Input is not a valid number.";
}