# PHP maths formular needed

Hi Guys!

I need some kind of formular for PHP to use in an if statement. The if statement if written out, should work like so:

``````
if(\$number >= 0 && \$number <= 3)
{

}
if(\$number >= 12 && \$number <= 15)
{

}
if(\$number >= 24 && \$number <= 27)
{

}
if(\$number >= 36 && \$number <= 39)
{

}

``````

Is there a better way to write this?

Couple of questions.

1. Is the logic within each if block going to be different? I assume it would be otherwise, you could make one massive if statement.
2. Is this the full set of logic, or is there more? As right now, it seems you want to check if \$number is one of 4 digits, add 9, check again, etc. This is predictable, so it could be written in various ways

You can use the `case` (switch) statement for this.

``````
switch (\$number) {
case 0:
case 1:
case 2:
case 3:
//do something
break;
case 12:
case 13:
case 14:
case 15:
// do something else
break;
case 24:
case 25:
case 26:
case 27:
//do something else more
break;
case 36:
case 37:
case 38:
case 39:
// do yet another something
break;
default: //NEVER, EVER, EVER forget the 'default'
// default action - display an error???
}

``````

Not quite as elegant as it is in other languages, though.

This whole situation just seems oddâ€¦ if you ask me.

Try this:

``````

\$a=array();
for(\$i2=0; \$i2<=3; \$i2++)
\$a = array_merge( \$a, range(\$i2*12, 3+\$i2*12) );

\$number = 25;
echo 'Testing: ', \$number, '<br />';

if( in_array(\$number, \$a) )
if(\$number < 4)
echo 'Yes 0, 1, 2 or 3';

else if(\$number < 16)
echo 'Yes 12,13,14 or 15';

else if(\$number < 28)
echo 'Yes 24,25,26 or 27';

else # if(\$number < 40)
echo 'Yes 36,37,38 or 39';

``````