i have started learning php and i saw a piece of code in php documentations that was bug reported but in the report page said that was not a bug. the code is :
$bar = 'bar';
$foo = 'foo';
if (isset($bar)):
if (isset($foo)) echo "Both are set.";
elseif (isset($foo)):
echo "Only 'foo' is set.";
else:
echo "Only 'bar' is set.";
endif;
if we put the nested if code in curly braces again that error is happening.
if (isset($bar)) :
if (isset($foo)) {
echo "Both are set.";
}
elseif (isset($foo)) :
echo "Only 'foo' is set.";
else :
echo "No one is set.";
endif;
the above code raise an error unexpected ":" and the reason of report page said this it is not a bug it was the mixed structure used in above code and php does not allowed and i accepted that but what makes this code go wrong. but why if we add another semicolon at the end of nested if statement it works perfectly. or why if we add else statement at the end of nested if statement error will gone. something like this :
if (isset($bar)) :
if (isset($foo)) {
echo "Both are set.";
} else {
echo "this is else";
}
elseif (isset($foo)) :
echo "Only 'foo' is set.";
else :
echo "No one is set.";
endif;
i will appreciate if anyone help i know this mixed structure is really bad but understanding the problem will help to prevent another mistakes. thanks a lot;
Aucun commentaire:
Enregistrer un commentaire