-1

In the PHP code when put $result variable more than 100. Then output should be "Your result is invalid". But it's showing "You have passed". Why? Can anybody help me please?

Check code here - https://pasteboard.co/HZkC73C.png

Striezel
  • 3,693
  • 7
  • 23
  • 37
  • 4
    Post formatted code in your question, don't attach images. – Devon Bessemer Feb 02 '19 at 18:19
  • Switch those conditions. – raina77ow Feb 02 '19 at 18:19
  • 3
    Because 130 is more than 33 so the first condition is being met. You haven't found a bug in PHP If statements. – Matthew Page Feb 02 '19 at 18:22
  • The output is correct. Your assumption that the code will execute the else if statement is incorrect because $result = 130. It will reach the else if statement when the $result is < 0. – Tim Strawbridge Feb 02 '19 at 18:26
  • Might you please [edit] your question to include your code as **text** rather than as a screenshot? It's required here not to to use images for this purpose, see [*Discourage screenshots of code and/or errors*](https://meta.stackoverflow.com/a/307500) and [*Why not upload images of code on SO when asking a question*](https://meta.stackoverflow.com/a/285557) for why. [How do I format my code blocks?](https://meta.stackexchange.com/q/22186) may also help. – dbc Feb 03 '19 at 01:34

2 Answers2

0

Cause your variable has value greater than 33 and greater than 100 in the same time, and it goes only into first IF statement block. If you need second statement to be valid - switch IF and ELSE IF blocks.

illia permiakov
  • 423
  • 3
  • 10
0
<?php  
$result=130;  
if($result >=30 && $result <=100){
echo "You have  passed";
}
elseif ($result < 0 || $result >100) {
echo "Your result is invalid";
}
else {
echo "fail";
}
?>