Page 1 of 1

Number comparison: Greater than x but less than y... how?

Posted: Mon Jun 26, 2006 9:49 am
by mattcooper
Hi guys,

I'm trying to write a simple game and need to create outcomes that depend on the value of $var.

If $var is between 100 & 110, event #1 happens;
If $var is between 111 & 120, event #2 happens;

etc...

Does anyone know a way of doing this with a switch() statement? If not, another simple way?

Thanks in advance!

Matt

Posted: Mon Jun 26, 2006 9:55 am
by feyd
if they fall into even buckets (say the 10 blocks you've posted) .. floor($var/10)

For forcing a value to a given range, I use

Code: Select all

$value = min(max($value, $low), $high);

Posted: Mon Jun 26, 2006 10:28 am
by mattcooper
many thanks again, Feyd, floor() works nicely for me... and is easy to work into a switch statement.

Posted: Mon Jun 26, 2006 10:40 am
by Jenk
If they are uneven or irregular categories/buckets, you can do something like:

Code: Select all

<?php

switch (true) {
    case (($value > 0) && ($value < 10)):
    //do something
    break;

//etc..

}

?>
Though it is messy and I personally would try not to use it.