I want to secure my page by checking if the value is digital (0,1,2,3) and if it is in the range from 0 to 120. I think ctype_digit function limits numbers, so
Here's a simple way:
function set_range($value, $minimum, $maximum) {
return min(max($minimum, $value), $maximum);
}
Here's what we're doing:
And here's a test:
// Check every fifth number between 0-60 and
// set output to within range of 20 to 40.
//
for ($i = 0; $i < 60; $i += 5) {
echo $i . " becomes " . set_range($i, 20, 40) . PHP_EOL;
}
If you want to check if a number is within a range, you could do this:
function in_range($value, $minimum, $maximum) {
return ($value >= $minimum) && ($value <= $maximum);
}
echo (in_range( 7, 20, 40)) ? "yes" : "no"; // output: no
echo (in_range(33, 20, 40)) ? "yes" : "no"; // output: yes