Possible Duplicate:
php if integer between a range?
Let's say $num = 5;
How do I test if $value
is anything in between +-3 of $num. In other words, how can I test if $value
is equal to any of these values 2,3,4 5 6,7,8
Possible Duplicate:
php if integer between a range?
Let's say $num = 5;
How do I test if $value
is anything in between +-3 of $num. In other words, how can I test if $value
is equal to any of these values 2,3,4 5 6,7,8
Two possible ways to do it:
$num - 3 <= $value && value <= $num + 3
abs($num - $value) <= 3
$mid = 5;
$range = 3;
$inRange = ($myval>=$mid-$range && $myval<=$mid+$range) ? TRUE : FALSE;
UPDATE I started throwin' out bass, she started throwin' back mid-range.
if ($value<=$num+3 && $value>=$num-3)
echo "$value is between +-3 of $num";
else
echo "$value is outside +-3 of $num";
try this:
$dif1 = $num - 3;
$dif2 = $num + 3;
if($dif1 <= $value){
if($dif2 <= $value){
echo "Your number in between +-3";
}
}
There is nothing big logic in this if you know the $num value take two variable $min and $max
set $min = $num - 3
set $max = $num + 3
and then with condition check your value..
$value > $min && $value < $max