Concept

Why does an extra semicolon make the conditional statement ineffective?

ComputerScienceOne / Computing a Logarithm

"This PHP code will run without error or warning. However, it will end up printing x is less than 10, even though x = 15! Recall that a conditional statement binds to the executable statement or code block immediately following it. In this case, we’ve provided an empty executable statement ended by the semicolon. The code is essentially equivalent to:\n\n$x = 15;\n\nif($x < 10) {\n\n}\n\nprintf(\"x is less than 10\n\n\");\n\nThis is obviously not what we wanted. The semicolon was bound to the empty executable statement and the code block containing the print statement immediately followed, but was not bound to the conditional statement which is why the print statement executed regardless of the value of x."

Related Ideas

Why does an extra semicolon make the conditional statement ineffective? | ComputerScienceOne | Bifalgorithm | Bifalgorithm