Why it won’t work? Appears Parse error: syntax error, unexpected ‘:’ … on line 7
$a = 0; $b = 1; $c = 3; $d = 4;
if ($a == $b):
if ($b == $c) {
// something
}
else:
$c = $a;
endif;
But if i change it to (just added else statement):
$a = 0; $b = 1; $c = 3; $d = 4;
if ($a == $b):
if ($b == $c) {
// something
} else {
// something
}
else:
$c = $a;
endif;
It works fine.
Why is that? isn’t it a PHP bug? Or there is a rule about if…else i should know?
Anyway, i’m using PHP 5.3.3 version.
I am not sure if I would call this is a bug, but I believe you’re having this issue because of a dangling else, in conjunction with your mixed if-else syntax:
Note how the
elseon line 5 is ambiguous: it could “belong” either to the first or secondifstatements.You can easily remove that ambiguity and fix your syntax error by adding a semicolon after your nested
if:On another note, please don’t use this syntax unless you want your fellow programmers to bludgeon you to death in your sleep.