If value is greater than and less than

Discussion in 'PHP' started by simstar, Jul 9, 2008.

  1. #1
    How do I create an if statement which uses both greater than and less than

    I want to display
    'Excellent Cheat' if the rating is between 0 and 1.5
    so >0 and <1.5

    'Good Cheat' if the rating is between 1.5 and 2.5
    so >=1.5 and <2.5

    'Ok Cheat' if the rating is between 2.5 and 3.5
    so >=2.5 and <3.5

    I dont know how to do this in php though i tried this:
    if ($overall<1.5) ($overall>0);
    {$CheatRating = "Excellent Cheat";}
    if ($overall<2.5) ($overall>1.5);
    {$CheatRating = "Good Cheat";}
    if ($overall<3.5) ($overall>2.5);
    {$CheatRating = "Ok Cheat";}
    if ($overall<4.5) ($overall>3.5);
    {$CheatRating = "Poor Cheat";}
    PHP:
    but it didnt seem to work as it found 'poor cheat' everytime.

    any help is very much appreciated. Thanks
     
    simstar, Jul 9, 2008 IP
  2. ForTheWin

    ForTheWin Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if ($overall<1.5 && $overall>0);
    {$CheatRating = "Excellent Cheat";}
    if ($overall<2.5 && $overall>1.5);
    {$CheatRating = "Good Cheat";}
    if ($overall<3.5 && $overall>2.5);
    {$CheatRating = "Ok Cheat";}
    if ($overall<4.5 && $overall>3.5);
    {$CheatRating = "Poor Cheat";}

    I think thats what your trying to do.
     
    ForTheWin, Jul 9, 2008 IP
  3. simstar

    simstar Peon

    Messages:
    467
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Looks about right thanks, I understand the code but just didnt know that & was needed. Little things like that can save me hours cheers :)
     
    simstar, Jul 9, 2008 IP
  4. ForTheWin

    ForTheWin Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Don't think you need the ; at the end of the if statement aswell.
     
    ForTheWin, Jul 9, 2008 IP
  5. Ikki

    Ikki Peon

    Messages:
    474
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi there,

    This should work too:

    
    $overall = 1.7;
    
    switch ($overall) {
    	case ((($overall > 0) and ($overall < 1.5))?$overall:false):
    		$cheatRating = "Excelent cheat";
    		break;
    	case ((($overall >= 1.5) and ($overall < 2.5))?$overall:false):
    		$cheatRating = "Good cheat";
    		break;
    	case ((($overall >= 2.5) and ($overall < 3.5))?$overall:false):
    		$cheatRating = "OK cheat";
    		break;
    	case ((($overall >= 3.5))?$overall:false):
    		$cheatRating = "Poor cheat";
    		break;
    	default:
    		$cheatRating = "Poor cheat";
    		break;
    }
    
    PHP:
    Result:
    $cheatRating = "Good cheat".
     
    Ikki, Jul 9, 2008 IP
  6. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #6
    There are times when it's easier on the brain just to use if rather than a switch/case/break set.

    if ( $overall < 1.5 && $overall > 0 )    $CheatRating = 'Excellent Cheat';
    if ( $overall < 2.5 && $overall >= 1.5 ) $CheatRating = 'Good Cheat';
    if ( $overall < 3.5 && $overall >= 2.5 ) $CheatRating = 'Ok Cheat';
    if ( $overall < 4.5 && $overall >= 3.5 ) $CheatRating = 'Poor Cheat';
    if ( $overall >= 4.5 )                   $CheatRating = '...';
    PHP:
     
    Danltn, Jul 9, 2008 IP