# elseif not working how it should

## 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
##### Share on other sites

That's what it should do, you're saying that if \$exp is greater than 3000 to echo 2. 99999 is greater than 3000.

##### 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

##### Share on other sites

But once the if is filled then it stops the loop, switch around your elseif's and it should work.

##### 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.
##### 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
##### 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.

##### Share on other sites

Thanks got it all working now using your way

## Create an account

Register a new account