I have this piece of code:
$a = false;
if ($a)
echo 'A'; if (false) echo 'B';
else echo 'C';
The else statement appears to changes “belongship” depending on the value of $a. If it’s true, it seems to interpret it as:
if ($a)
{
echo 'A';
if (false)
{
echo 'B';
}
else
{
echo 'C';
}
}
And prints AC. But if $a = false, it seems to be interpreting it as:
if ($a)
{
echo 'A';
if (false)
{
echo 'B';
}
}
else
{
echo 'C';
}
And prints C.
Am I missing something here?
Your first code reads as
When you do not use braces, only the next statement (statement, not line) gets executed as part of that control structure. It is not ambiguous as this behaviour is clearly defined in the programming language.