heeha Posted November 25, 2016 Share Posted November 25, 2016 I am trying to echo the right answer but I always get the wrong one. <?php $gplus= 344; if($gplus >= 1){ echo "Get answer 1."; } else if($gplus >= 50) { echo " Get answer 2."; } else if ($gplus >= 100) { echo " Get answer 3."; } else if($gplus >= 200) { echo "Get answer 4"; } else if($gplus >= 500){ echo "Get answer 5."; } else if($gplus >= 1000){ echo "Get answer 5."; } else if($gplus >= 2000) { echo " Get answer 6."; } else if($gplus >= 3000) { echo " Get Answer 7."; } else { echo "We didn't find any answers."; } ?> $gplus is 344, and it should echo the anser " Get answer 4" but it always echo " Get answer 1". If i change the value of $gplus = 501, it still echo the answer "Get answer 1". What wrong am i doing? Quote Link to comment https://forums.phpfreaks.com/topic/302613-wrong-operator/ Share on other sites More sharing options...
Joshinki Posted November 25, 2016 Share Posted November 25, 2016 <?php $gplus= 344; if($gplus >= 1 AND $gplus < 49){ echo "Get answer 1."; } else if($gplus >= 50 AND $gplus < 99) { echo " Get answer 2."; } else if ($gplus >= 100 AND $gplus < 199) { echo " Get answer 3."; } else if($gplus >= 200 AND $gplus < 499) { echo "Get answer 4"; } else if($gplus >= 500 AND $gplus < 999){ echo "Get answer 5."; } else if($gplus >= 1000 AND $gplus < 1999){ echo "Get answer 5."; } else if($gplus >= 2000 AND $gplus < 2999) { echo " Get answer 6."; } else if($gplus >= 3000) { echo " Get Answer 7."; } else { echo "We didn't find any answers."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/302613-wrong-operator/#findComment-1539679 Share on other sites More sharing options...
Barand Posted November 25, 2016 Share Posted November 25, 2016 or use the same tests as before but test the values in descending order. Currently, if the value is 3001 then that is > 1, so it goes no further. Quote Link to comment https://forums.phpfreaks.com/topic/302613-wrong-operator/#findComment-1539685 Share on other sites More sharing options...
kicken Posted November 25, 2016 Share Posted November 25, 2016 PHP will enter the first branch that is true. 344 is greater than 1 so that condition is true and that is the branch that will be taken. You either need to add a second condition to specify a range as shown or you need to put your higher numbers first. $gplus= 344; if($gplus >= 3000){ echo " Get Answer 7."; } else if($gplus >= 2000) { echo " Get answer 6."; } else if ($gplus >= 1000) { echo "Get answer 5."; } else if($gplus >= 500) { echo "Get answer 5."; } else if($gplus >= 200){ echo "Get answer 4"; } else if($gplus >= 100){ echo " Get answer 3."; } else if($gplus >= 50) { echo " Get answer 2."; } else if($gplus >= 1) { echo "Get answer 1."; } else { echo "We didn't find any answers."; } Quote Link to comment https://forums.phpfreaks.com/topic/302613-wrong-operator/#findComment-1539686 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.