Lei Lionel Lei Lionel - 6 months ago 17
PHP Question

Verify if a value is included between two numbers

I am working with PHP and I want to verify if a specific number is between 2 numbers and if it returns

TRUE
then we display a message

if($number is between $value1 and $value2){
echo 'Ok';
}else{
echo 'not Ok';
}


PS:
$number
can be
$value1
or
$value2

Answer
function isBetween($number, $value1, $value2) {
    return $number >= $value1 && $number <= $value2;
}

. . . .

if(isBetween(1, 0.5, 2.5))
   echo "Ok";
else 
   echo "Not Ok";

Pretty self-explanatory

Comments