Is there a way to test a range without doing this redundant code:
if ($int>$min && $int<$max)
?
Like a function:
function testRange($int,$min,$max){
return ($min<$int && $int<$max);
}
usage:
if (testRange($int,$min,$max))
?
Does PHP have such built-in function? Or any other way to do it?
Tested your 3 ways with a 1000000-times-loop.
t1_test1:
($val >= $min && $val <= $max)
: 0.3823 mst2_test2:
(in_array($val, range($min, $max))
: 9.3301 mst3_test3:
(max(min($var, $max), $min) == $val)
: 0.7272 msT1 was fastest, it was basicly this:
Most of the given examples assume that for the test range [$a..$b], $a <= $b, i.e. the range extremes are in lower - higher order and most assume that all are integer numbers.
But I needed a function to test if $n was between $a and $b, as described here:
There is an easy way to test.
I base the test it in the fact that
($n-$a)
and($n-$b)
have different signs when $n is between $a and $b, and the same sign when $n is outside the $a..$b range.This function is valid for testing increasing, decreasing, positive and negative numbers, not limited to test only integer numbers.