This question already has an answer here:
I've always been wondering about this since I began learning PHP. As many other new-beginners, I placed semicolons all over the place, and for some reason, placing them after an if statement compiles and runs, but not like one would expect.
Example:
<?php
$foo = "bar";
if ($foo == "bar") // Correct way
echo "OK";
if ($foo == "bar"); // Incorrect way
echo "OK";
if ($foo == "derp"); // Incorrect way again
echo "NO";
?>
This will output:
OKOKNO
Why is this? What is the intended usage for this? Just a twerk left in the code that for some reason has never been fixed? It looks like the compiler just skips the entire if-statement all together.
</div>
Because the PHP interpreter is looking for one statement after the if(...)
to execute if the condition is evaluated to true
. Appending the semicolon tells the interpreter that you're done with the statement. You've effectively told PHP to execute a no-op statement when the condition holds true. The echo
isn't even part of the if-statement anymore.
It's equivalent to writing
if($foo == "bar"){
}
echo "OK";
The second and third if statements are seen as empty statements due to the semi colon. Empty statements really do nothing.
because in your case a semi-colon is considered as empty statement, so your code is interpreted as following:
<?php
$foo = "bar";
if ($foo == "bar")
echo "OK";
if ($foo == "bar")
; // empty statement
echo "OK";
if ($foo == "derp")
; // empty statement
echo "NO";
?>
This drove me INSANE when I first started learning Java, caused so many bugs... I still have nightmares.
I'm assuming the reason why it skips the if statement is because the semicolon is telling the compiler that the block has ended.
The following would work.
if ($foo == "bar") echo = "OK";
What if you just replaced the variable assignment?
if ($foo == "bar") ;
echo = "OK";
That's still legal code, but it just says, if this conditional is true, then do nothing. The code that was suppose to be executed as part of the if-block will always be executed.
Cheers,
As mentioned in the other answers, the "how" is because the semicolon is evaluated as an empty statement, then the next line is "after" the if condition so gets executed.
There are probably a lot of things that are syntactically "valid" but have no real use, for instance you can technically just stick a variable on the line followed by a semicolon, it would not do a thing but it would be valid... In this situation though, there is a real world use for this: Use the if conditionals as a way to execute a series of functions, but stop at the first one that returns true. Something like:
//Go until it gets to one that returns true...
if (firstCall() || secondCall() || thirdCall()) ;
In that example, it will always call firstCall()
. If that function returns true, PHP stops there as it doesn't matter what the others evaluate to, it knows the entire statement is true. If it returns false, it keeps going until it does come across one that returns true (until it makes it through all of the parts). In other words, the above would be short-hand for this:
if (!firstCall()) {
if (!secondCall()) {
thirdCall();
}
}
You could do a similar solution using AND to make it stop at the first method that returns false.