elseif/else if

elseif, 이 이름에서 알수 있듯이, ifelse의 조합이다. else처럼 이 구문은 if절 다음에 와서 원래 if표현식이 FALSE와 같은 경우에 다른 구문을 수행한다. 그러나, else와는 달리 elseif조건 표현식이 TRUE일 때만 대체 표현식을 수행할것이다. 예를 들면 다음 코드는 a는 b보다 크다, a는 b와 같다a는 b보다 작다을 출력할것이다.

<?php
if ($a $b) {
    echo 
"a는 b보다 크다";
} elseif (
$a == $b) {
    echo 
"a는 b와 같다";
} else {
    echo 
"a는 b보다 작다";
}
?>

같은 if절 안에 몇개의 elseif절이 존재할수 있다. 가장 먼저 TRUE가 되는 elseif표현식이 수행될것이다. PHP에서는 'else if' (두 단어)로 쓸수 있고 'elseif' (한 단어) 와 방식은 같다. 문장적(syntactic)으로는 다르다 (C에 익숙하다면, 이것은 같은 방식이다) 그러나 그 둘 모두 완전히 같은 결과를 보여줄것이다.

elseif절은 선행 if 표현식과 다른 elseif표현식이 FALSE이고, 이 elseif표현식이 TRUE일때만 수행된다.

Note: elseifelse if은 위 예제처럼 대괄호를 사용할 때 정확히 같은 구문으로 간주됩니다. if/elseif 조건을 콜론을 사용해서 정의할 때, else if 처럼 두 단어로 나눠서는 안됩니다. PHP는 처리 오류로 실패합니다.

<?php

/* 부적합한 방법: */
if($a $b):
    echo 
$a." is greater than ".$b;
else if(
$a == $b): // 컴파일 되지 않습니다.
    
echo "위 줄은 처리 오류를 일으킵니다.";
endif;


/* 적합한 방법: */
if($a $b):
    echo 
$a." is greater than ".$b;
elseif(
$a == $b): // 단어가 붙어 있는 점에 주의.
    
echo $a." equals ".$b;
else:
    echo 
$a." is neither greater than or equal to ".$b;
endif;

?>

add a note add a note

User Contributed Notes 3 notes

up
2
Vladimir Kornea
17 years ago
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.
up
-1
mparsa1372 at gmail dot com
3 years ago
The if...elseif...else statement executes different codes for more than two conditions.

Syntax:

if (condition) {
  code to be executed if this condition is true;
} elseif (condition) {
  code to be executed if first condition is false and this condition is true;
} else {
  code to be executed if all conditions are false;
}

Example:
Output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!":

<?php
$t
= date("H");

if (
$t < "10") {
  echo
"Have a good morning!";
} elseif (
$t < "20") {
  echo
"Have a good day!";
} else {
  echo
"Have a good night!";
}
?>
up
-12
qualitycoder
9 years ago
The reason 'else if' (with a space) works with traditional syntax and not colon syntax is because of a technicality.

<?php
 
if($var == 'Whatever') {

  } else if(
$var == 'Something Else') {

  }
?>

In this instance, the 'else if' is a shorthand/inline else statement (no curly braces) with the if statement as a body. It is the same things as:

<?php
 
if($var == 'Whatever') {

  } else {
      if(
$var == 'Something Else') {

      }
  }
?>

If you were to write this with colon syntax, it would be:

<?php
 
if($var == 'Whatever'):

  else:
      if(
$var == 'Something Else'):

      endif;
  endif;
?>
To Top