Jump to content

elseif not working how it should


joemorris86
 Share

Recommended Posts

Hi, the code im using below doesnt seem to be working properly.To test it, i set exp to 99999 but for some reason it only echo's '2' instead of 4.Could anyone point out what I have done wrong? Thanks for any help

<?phpif ($exp < 1000) {  echo '1';} elseif ($exp > 3000) {  echo '2';} elseif ($exp > 8000) { echo '3';} elseif ($exp > 16000) { echo '4';}?>

Edited by joemorris86
Link to comment
Share on other sites

But then the next ifelse is saying if its over 8000, echo 3, but its still on 2.Its for a game, exp starts of at 0, then every time an enemy is defeated, it adds 100 more to the exp, then when exp reaches a certain amount, i want it to upgrade the character level, thats what i thought this code would doif experience is greater than 3000 echo characters level = 2if experience is greater than 8000 echo characters level = 4etc etc

Link to comment
Share on other sites

But then the next ifelse is saying if its over 8000, echo 3, but its still on 2.
Yeah, but it's not going to get there because you're telling it to echo 2 if $exp is greater than 3000. You're not telling it to echo only if it is greater than 3000 AND less than 8000, the only condition is that it is greater than 3000. If you want it to also be less than 8000, then tell it that. It's only going to do what you tell it to do.
Link to comment
Share on other sites

Yeah, but it's not going to get there because you're telling it to echo 2 if $exp is greater than 3000. You're not telling it to echo only if it is greater than 3000 AND less than 8000, the only condition is that it is greater than 3000. If you want it to also be less than 8000, then tell it that. It's only going to do what you tell it to do.
ah, that does make sense.should something like this work ?
<?phpif ($exp < 1000) {  echo '1';} elseif (($exp > 3000) && ($exp < 8000)) {  echo '2';} elseif (($exp > 8000) && ($exp < 16000)) { echo '3';} elseif (($exp > 16000) && ($exp < 25000)) { echo '4';}?>

Edited by joemorris86
Link to comment
Share on other sites

Yeah, that will work. You can also do it backwards and check from highest to lowest:

if ($exp > 16000)  echo '4';elseif ($exp > 8000)  echo '3';elseif ($exp > 3000)  echo '2';else  echo '1';

In that case you don't need the extra conditions because it will only move on if $exp is less than what it's currently checking. So when you're checking if you want to output '3', you don't need to also check if it's less than 16000 because the previous condition checked for that (it only moves on from the first condition if $exp is less than or equal to 16000).Similarly:

if ($exp < 1000) {  echo '1';} elseif ($exp <= 8000) {  echo '2';} elseif ($exp <= 16000) { echo '3';} elseif ($exp <= 25000) { echo '4';}

The key is to keep the operator (less than / greather than) the same. Your first code wasn't working correctly because you checking for less than first, then greater than.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...