There is no good way to interpret the dangling else. One must pick a way and apply rules based on that.
Since there is no endif before an else, there is no easy way for PHP to know what you mean.
elseif
Η εντολή elseif, όπως λέει και το όνομα της, είναι ένας συνδυασμός των if και else. 'Οπως το και else, έχει ως επέκταση μία if έκφραση με σκοπό να εκτελέσει μια διαφορετική έκφραση σε περίπτωση που η αρχική if συνθήκη πάρει την τιμή FALSE. Παρόλαυτα, σε αντίθεση με το else, θα εκτελέσει αυτή την εναλλακτική έκφραση μόνο αν η elseif υποθετική συνθήκη πάρει την τιμή TRUE. Για παράδειγμα, το ακόλουθο κομμάτι κώδικα θα εμφανίσει a is bigger than b, a equal to b or a is smaller than b:
<?php
if ($a > $b) {
print "a is bigger than b";
} elseif ($a == $b) {
print "a is equal to b";
} else {
print "a is smaller than b";
}
?>
Μπορούν να υπάρχουν πολλά elseifs μέσα στην ίδια έκφραση if. Η πρώτη elseif έκφραση (αν υπάρχει) που θα πάρει την τιμή TRUE θα είναι και αυτή που θα εκτελεστεί. Στην PHP, μπορείτε επίσης να γράψετε 'else if' (σε δυο λέξεις) και η συμπεριφορά να είναι όμοια με αυτή του 'elseif' (μία λέξη). Η συντακτική έννοια είναι ελαφρώς διαφορετική (αν έχετε οικειότητα με τη C, είναι ακριβώς η ίδια συμπεριφορά) αλλά το τελικό αποτέλεσμα είναι ότι και οι δυο εκφράσεις θα καταλήξουν στην ίδια ακριβώς συμπεριφορά.
Η έκφραση elseif εκτελείται μόνο αν η προηγούμενη έκφραση if και οποιεσδήποτε προηγούμενες εκφράσεις elseif έχουν πάρει την τιμή FALSE, και η τρέχουσα έκφραση elseif πάρει την τιμή TRUE.
elseif
27-Dec-2006 09:59
The parser doesn't handle mixing alternative if syntaxes as reasonably as possible.
The following is illegal (as it should be):
<?
if($a):
echo $a;
else {
echo $c;
}
?>
This is also illegal (as it should be):
<?
if($a) {
echo $a;
}
else:
echo $c;
endif;
?>
But since the two alternative if syntaxes are not interchangeable, it's reasonable to expect that the parser wouldn't try matching else statements using one style to if statement using the alternative style. In other words, one would expect that this would work:
<?
if($a):
echo $a;
if($b) {
echo $b;
}
else:
echo $c;
endif;
?>
Instead of concluding that the else statement was intended to match the if($b) statement (and erroring out), the parser could match the else statement to the if($a) statement, which shares its syntax.
While it's understandable that the PHP developers don't consider this a bug, or don't consider it a bug worth their time, jsimlo was right to point out that mixing alternative if syntaxes might lead to unexpected results.
