Page 1 of 1

between

Posted: Mon Oct 09, 2006 5:16 pm
by Luke
Is there a native php function that tells you whether a number is between x and y... something like this?

Code: Select all

if(between($num, $low, $high)) // Do something
I couldn't find it if it exists

Posted: Mon Oct 09, 2006 5:23 pm
by feyd
no.

Code: Select all

if (max($num, $low) == min($num, $high))

Posted: Mon Oct 09, 2006 5:31 pm
by Luke
perfect... thanks feyd.

Posted: Mon Oct 09, 2006 8:21 pm
by s.dot
perhaps...

Code: Select all

if(in_array($x, range($y,$z))

Posted: Tue Oct 10, 2006 2:42 am
by volka
to costly. I'd stick with
feyd wrote:no.

Code: Select all

if (max($num, $low) == min($num, $high))

Posted: Tue Oct 10, 2006 2:49 am
by s.dot
too costly? depends on how big the range is ;)

but yes, i would also stick with min/max

Posted: Tue Oct 10, 2006 3:16 am
by Oren
I'd stick with:

Code: Select all

if ($num > $low && $num < $high)
I didn't make any tests, but I'm almost sure it'd be faster than the other solutions because the other solutions mentioned above call to 2 functions, while the above calls non.

Edit:

After a very short and rough test I can say for sure: Don't use scottayy's way (no offense of course :P).
Second, according to the test, my way is ±8 times faster than feyd's way. But again, it was a rough test and maybe a better and more serious test will have different results (although I doubt it).