0

# Elseif Expression I need help

Don’t understand why IN A ELSEIF EXPRESSION this: elseif ($y=21 && $y<22) Is different than this: elseif ($y<22 && $y=21)

6 ответов

+ 3

Your code is very different to what you have in this question. Your 2nd elseif is ($y>=22 && $y<=95).

+ 3

1) testing if y is equal to 21 and less than 22 doesn't make sense. If y equals 21, it will always be less than 22
2) use == for comparison, not =.
if($y == 21) checks if y is equal to 21.
if($y = 21) assigns 21 to y and returns true.
So if($y = 21 && $y < 22) will always be true (y will be set to 21 first and then it'll check if y is less than 22)
whereas in if($y < 22 && $y = 21), the second part (set y to 21) will only be executed if the first part (y < 22) is true

+ 2

Did you try it and get a different result? The only time this would be True is when y = 21. The test for y<22 seems like a waste of time.

0

yes I get different result. if you check the excercise I made named "ifelse alcoholic" it prints different results

0

try changing the first elseif with the examples I provided and, one way will works and the other wont

0

thanks a los Anna I used = the first time and did not work With == did just great. I will search deeper the differences about this.