Comparing 3 Numeric Values

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
annnthony
Forum Newbie
Posts: 10
Joined: Mon Jun 30, 2008 11:44 pm

Comparing 3 Numeric Values

Post by annnthony »

Is there any way to use PHP to compare 3 numbers and check that they are within a certain range? I'm sure I could do some very lengthy if statements to compare, but if there's a way to just use an array or something else clever, advice would be great.

For example, if I'd want the numbers to all be within 10 of each other, and I had the numbers 39, 41, and 44... I'm not sure how to explain without using code, but hopefully the first little paragraph will be helpful enough to explain stuff.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Comparing 3 Numeric Values

Post by Ollie Saunders »

Code: Select all

// assume $nums is an array
if (max($nums) - min($nums) > RANGE_LIMIT) {
   echo 'Whoa there little man peach';
} else {
   echo 'Having good times, almost as good as that soup.';
}
Post Reply