Note that } elseif() { is somewhat faster than } else if() {
===================================
Test (100,000,000 runs):
<?php
$start = microtime(true);
for($i = 0; $i < 100000000; $i++) {
if(2 === 0) {} else if(2 === 1) {} else {}
}
$end = microtime(true);
echo "1: ".($end - $start)."\n";
unset($start, $end);
$start = microtime(true);
for($i = 0; $i < 100000000; $i++) {
if(2 === 0) {} elseif(2 === 1) {} else {}
}
$end = microtime(true);
echo "2: ".($end - $start);
?>
===================================
Result (depending on hardware configuration):
1: 20.026723146439
2: 20.20437502861
elseif/else if
elseif, isminden de anlaşılacağı gibi,
if ve else deyimlerinin biraraya
gelmesiyle oluşur. else gibi, orijinal
if ifadesinin FALSE döndürdüğü durumda farklı bir
deyimin çalıştırılması için kullanılır. Ancak,
else'ten farklı olarak, ilgili deyimi sadece
kendisine ait koşullu ifade TRUE değerini döndürdüğü takdirde
çalıştıracaktır. Örneğin, aşağıdaki kod a büyüktür
b, a eşittir b ya da
a küçüktür b iletisini
görüntüleyecektir:
<?php
if ($a > $b) {
echo "a büyüktür b";
} elseif ($a == $b) {
echo "a eşittir b";
} else {
echo "a küçüktür b";
}
?>
Aynı if deyimine ait birden çok
elseif bulunabilir. İlk önce hangi
elseif deyimi TRUE değerini döndürürse (eğer
döndüren varsa) o deyim çalıştırılır. Bu deyimi 'elseif' biçiminde tek
sözcük olarak kullanabileceğiniz gibi, 'else if' biçiminde iki sözcük
olarak da kullanabilirsiniz, sonuç aynı olacaktır. Sözdiziminin
anlamlandırılması açısından küçük bir farklılık olsa da (C diline
aşinaysanız, onda da böyledir) sonuç olarak her ikisi de aynı sonucu
üretecektir.
elseif ifadesi yalnızca kendisinden önceki
if ifadesi ve bu ifadeye bağlı kendisinden önce gelen
diğer tüm elseif ifadeleri FALSE ile
sonuçlandığında çalıştırılır ve TRUE olarak değerlendirilir.
Bilginize: elseif ve else if, yukarıdaki örnekten anlaşılacağı üzere sadece kaşlı ayraçlar kullanıldığı zaman tamamen aynı şekilde ele alınacaktır. if ve elseif için iki noktalı sözdizimi kullanıldığında elseif'i iki sözcüğe ayırmamanız gerekir, yoksa PHP bir çözümleme hatası vererek başarısız olur.
<?php
/* Yanlış Yöntem: */
if($a > $b):
echo $a." büyüktür ".$b;
else if($a == $b): // Bu derlenmez.
echo "Üstteki satır bir çözümleme hatasına yol açar.";
endif;
/* Doğru Yöntem: */
if($a > $b):
echo $a." büyüktür ".$b;
elseif($a == $b): // Sözcüklerin birleşik oluşuna dikkat!
echo $a." eşittir ".$b;
else:
echo $a." ne büyük ne de eşittir ".$b;
endif;
?>
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.
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.
