views:

232

answers:

7

Hay all, I have an if statement. How could I make this as short as possible?

 if ( $a < 0 ){
   if( $a > -2 ){
    echo "standard";
   }elseif( $a <= -2 && $a > -4 ){
    echo "thin";
   }elseif( $a <= -4 ){
    echo "super thin";
   }

 }else{
  if( $a < 2 ){
    echo "standard";
  }
  if( $a > 2.01 && $a <= 4){
    echo "thin";
  }
  if( $a > 4.01 && $a <= 8.00){
    echo "super thin";
  }
}

EDIT: basically $a will be any number (positive or negative) and we match it like this.

If the value is

  • +0.00 to +/- 2.00 – Standard
  • +/- 2.25 to +/- 4.00 – Thin
  • +/- 4.25 to +/- 8.00 – Super Thin

Extra marks for anyone who knows what this might be used for :)

+10  A: 

You can shorten it by using the absolute value:

$b = abs($a);
if ($b <= 2) {
    echo "standard";
} else if ($b <= 4) {
    echo "thin";
} else if ($b <= 8) {
    echo "super thin";
}
Gumbo
I think this is what the OP wants. Notice, however, that the original code produces no output for values > 8.
Martin v. Löwis
n1313
And also not for values between 2 and 2.01 or between 4 and 4.01 - but I doubt that's actually intended behaviour.
Michael Borgwardt
So, wait, you essentially saw reiner's answer, realized that his idea of not including the $b >= 2 in the else if was good, and swiped it for your own answer?
R. Bemrose
Even after your edit your code is wrong, as it doesn't give "super thin" for -100.
n1313
I dont think the value will ever be more than (+/-) 20.
dotty
Yeah, I read your edit. Your code isn't doing what you want :)
n1313
@R. Bemrose: The different conditions for negative and positive numbers were a little bit confusing. I needed some time to figure it out what boundaries he meant.
Gumbo
+6  A: 

What about this:

 $a = abs($a);
 if($a<=2) echo "standard";
 elseif($a<=4) echo "thin";
 elseif($a<=8) echo "super thin";
Toad
What about -100?
n1313
@n1313: The question doesn't give what anything less than -8 or greater than 8 should be. If it did, then an else condition could be added to it.
R. Bemrose
A: 
if ( $a < 0 )
{
    if( $a > -2 ) echo "standard";
    elseif($a > -4) echo "thin";
    else echo  "super thin";
}
else
{
  if( $a < 2 ) echo "standard";
  elseif( $a > 2.01 && $a <= 4) echo "thin";
  elseif( $a > 4.01 && $a <= 8.00) echo "super thin";
}
Lizard
A: 

Its always better to use switch rather than else if! It makes the code cleaner and extensible.

switch($a) {
    case (-2 < $a):
    case ($a < 2):
        $r = 'Standard';
    break;

    case ( 2.25 > $a):
    case (-2.25 < $a):
    case ($a <  4):
    case ($a > -4):
        $r = 'Thin';
    break;


    case ( 4.25 > $a):
    case (-4.25 < $a):
    case ($a <  8):
    case ($a > -8):
        $r = 'Super Thin';
    break;

    default:
        $r = 'Out of range';
} 

echo $r;


The case expression may be any expression that evaluates to a simple type, that is, integer or floating-point numbers and strings. Arrays or objects cannot be used here unless they are dereferenced to a simple type.

Taken from the documentation at http://us3.php.net/manual/en/control-structures.switch.php

powtac
Except you can't use switch for ranges.
Michael Borgwardt
`$x < $y < $z` isn't a valid expression in php.
ringmaster
+1  A: 

Or you can keep the actual ranges, allow the range ends to be different (rather than just the absolute values), and allow easy inclusion of additional ranges:

$ranges = array(
  'standard' => array(-2, 2),
  'thin' => array(-4, 4),
  'super thin' => array(null,8),
);
foreach($ranges as $key => $range) {
  if((is_null($range[0]) || $a => $range[0]) && (is_null($range[1]) || $a <= $range[1])) {
    echo $key;
    break;
  }
}

The first matching range in the list is the one that qualifies.

ringmaster
A: 

I don't use PHP much so I tend to get the syntax a bit wrong, but perhaps something along this general order would work:

strings[] = {"standard", "thin", "super thin"};

$a = abs($a); if ($a < 8) echo strings[(int)($a/2)];

Jerry Coffin
+4  A: 

This is about as simple as it gets:

<?php
$b = abs($a);
$ranges = array(
  'standard' => 2,
  'thin' => 4,
  'super thin' => 8
);
foreach($ranges as $range=>$max) {
    $size = $range;
    if($b > $max) continue;
    break;
}
?>
KyleFarris
I don't understand what enamors novices about elseif, since it's hardly ever the best solution. Voting this up. My answer is similar, and also includes custom ranges and accounts for values less than -8.
ringmaster
@ringmaster: If you convert to absolute value, this will always work, regardless of sign and it will work for values more than the absolute value of +/-8. For instance, if the value is `-9` (i.e. `9`... because of `abs()`), `$size` would be set to `standard`, fail, then `"thin"`, fail again, then `"super thin"`, and would fail once more... but then the loop would be over and `$size` would still be `"super thin"`.
KyleFarris