else

(PHP 4, PHP 5, PHP 7, PHP 8)

ある条件が満たされている場合にある文を実行し、 その条件が満たされていない場合に別の文を実行したいと考えた ことが度々あるかと思います。 このためにelseがあります。 elseは、if文における式の値が falseの場合にある文を 実行するようにif文を拡張します。 例えば、次のコードは、$a$bよりも大きい場合に aはbより大きいと表示し、 そうでない場合に、 aはbよりも大きくないと表示します。

<?php
if ($a > $b) {
echo
"aはbよりも大きい";
} else {
echo
"aはbよりも大きくない";
}
?>
else 文は、if式が falseと評価された場合のみ実行されます。 また、elseif式がある場合には、それも falseと評価された場合にのみ実行されます。 (elseif参照)

注意: else がぶら下がる場合

if-else 文がネストした場合、 else は常に、もっとも近い if 文に関連付けられます。

<?php
$a
= false;
$b = true;
if (
$a)
if (
$b)
echo
"b";
else
echo
"c";
?>
どうインデントされているかに関わらず (PHP では、インデントは結果に影響しません)、 elseif ($b) に関連付けられます。 よって、この例は何も出力しません。 この振る舞いが正しいことに依存している場合、 曖昧さを解決するために波括弧を使うことを推奨します。

add a note

User Contributed Notes 2 notes

up
20
dormeydo at gmail dot com
17 years ago
An alternative and very useful syntax is the following one:statement ? execute if true : execute if falseThs is very usefull for dynamic outout inside strings, for example:print('$a is ' . ($a > $b ? 'bigger than' : ($a == $b ? 'equal to' : 'smaler than' )) .  '  $b');This will print "$a is smaler than $b" is $b is bigger than $a, "$a is bigger than $b" if $a si bigger and "$a is equal to $b" if they are same.
up
11
Caliban Darklock
20 years ago
If you're coming from another language that does not have the "elseif" construct (e.g. C++), it's important to recognise that "else if" is a nested language construct and "elseif" is a linear language construct; they may be compared in performance to a recursive loop as opposed to an iterative loop. <?php$limit=1000;for($idx=0;$idx<$limit;$idx++)  { $list[]="if(false) echo \"$idx;\n\"; else"; }$list[]=" echo \"$idx\n\";";$space=implode(" ",$list);| // if ... else if ... else$nospace=implode("",$list); // if ... elseif ... else$start=array_sum(explode(" ",microtime()));eval($space);$end=array_sum(explode(" ",microtime()));echo $end-$start . " seconds\n";$start=array_sum(explode(" ",microtime()));eval($nospace);$end=array_sum(explode(" ",microtime()));echo $end-$start . " seconds\n";?>This test should show that "elseif" executes in roughly two-thirds the time of "else if". (Increasing $limit will also eventually cause a parser stack overflow error, but the level where this happens is ridiculous in real world terms. Nobody normally nests if() blocks to more than a thousand levels unless they're trying to break things, which is a whole different problem.)There is still a need for "else if", as you may have additional code to be executed unconditionally at some rung of the ladder; an "else if" construction allows this unconditional code to be elegantly inserted before or after the entire rest of the process. Consider the following elseif() ladder:<?phpif($a) { conditional1(); }elseif($b) { conditional2(); }elseif($c) { conditional3(); }elseif($d) { conditional4(); }elseif($e) { conditional5(); }elseif($f) { conditional6(); }elseif($g) { conditional7(); }elseif($h) { conditional8(); }else { conditional9(); }?>To insert unconditional preprocessing code for $e onward, one need only split the "elseif":<?phpif($a) { conditional1(); }elseif($b) { conditional2(); }elseif($c) { conditional3(); }elseif($d) { conditional4(); }else {....unconditional();....if($e) { conditional5(); }....elseif($f) { conditional6(); }....elseif($g) { conditional7(); }....elseif($h) { conditional8(); }....else { conditional9(); }}?>The alternative is to duplicate the unconditional code throughout the construct.
To Top