All basic PHP functions which i tried returned unexpected results. I would just like to check whether some variable only contains numbers. For example: when i spread my script to the public i cannot require users to only use numbers as string or as integer. For those situation i wrote my own function which handles all inconveniences of other functions and which is not depending on regular expressions. Some people strongly believe that regular functions slow down your script.
The reason to write this function:
1. is_numeric() accepts values like: +0123.45e6 (but you would expect it would not)
2. is_int() does not accept HTML form fields (like: 123) because they are treated as strings (like: "123").
3. ctype_digit() excepts all numbers to be strings (like: "123") and does not validate real integers (like: 123).
4. Probably some functions would parse a boolean (like: true or false) as 0 or 1 and validate it in that manner.
My function only accepts numbers regardless whether they are in string or in integer format.
<?php
/**
* Check input for existing only of digits (numbers)
* @author Tim Boormans <info@directwebsolutions.nl>
* @param $digit
* @return bool
*/
function is_digit($digit) {
if(is_int($digit)) {
return true;
} elseif(is_string($digit)) {
return ctype_digit($digit);
} else {
// booleans, floats and others
return false;
}
}
?>