This tutorial will show you how to build a function that will validate if a number falls between a specific range of values. To use this function, provide a maximum and a minimum number, along with the number that will be checked. Website developers can use this to validate ages etc…
The function to check if value is within a two number range
function numberBetween($numToCheck, $high, $low) {
if($numToCheck <= $low) return false;
if($numToCheck >= $high) return false;
return true;
}
How to call the function
if (numberBetween(35, 100, 20)) {
echo 'The value is in the range!';
} else {
echo 'The value is outside the range!';
}
In this example, the value returned would be true because 35 is between 100 and 20.
I hope this data validation function will help you in your website development endeavors. It should simplify your development of a function that checks to see if a number (integer) falls between two values (range of numbers).



1 Comment
nice…
thanx!!!!!