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)

14th Apr 2019, 2:24 AM
Jorge Cueva
Jorge Cueva - avatar
6 Réponses
+ 3
Your code is very different to what you have in this question. Your 2nd elseif is ($y>=22 && $y<=95).
14th Apr 2019, 4:14 AM
Duncan
Duncan - avatar
+ 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
14th Apr 2019, 5:17 AM
Anna
Anna - avatar
+ 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.
14th Apr 2019, 4:05 AM
Duncan
Duncan - avatar
0
yes I get different result. if you check the excercise I made named "ifelse alcoholic" it prints different results
14th Apr 2019, 4:10 AM
Jorge Cueva
Jorge Cueva - avatar
0
try changing the first elseif with the examples I provided and, one way will works and the other wont
14th Apr 2019, 4:28 AM
Jorge Cueva
Jorge Cueva - avatar
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.
14th Apr 2019, 5:46 AM
Jorge Cueva
Jorge Cueva - avatar