Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/302613-wrong-operator/
Share on other sites


<?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.";
}
?>
Link to comment
https://forums.phpfreaks.com/topic/302613-wrong-operator/#findComment-1539679
Share on other sites

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.";
}
Link to comment
https://forums.phpfreaks.com/topic/302613-wrong-operator/#findComment-1539686
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.