between

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

between

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

no.

Code: Select all

if (max($num, $low) == min($num, $high))
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

perfect... thanks feyd.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

perhaps...

Code: Select all

if(in_array($x, range($y,$z))
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

to costly. I'd stick with
feyd wrote:no.

Code: Select all

if (max($num, $low) == min($num, $high))
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

too costly? depends on how big the range is ;)

but yes, i would also stick with min/max
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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).
Post Reply