PHP / Tutorial / 조건문 / if, elseif, else

if

설명

if로 조건을 만족할 때 실행할 작업을 정한다.

문법

if ( condition ) {
  statement;
}

condition이 TRUE이면 statement을 실행하고, FALSE이면 실행하지 않는다.

중괄호 대신 콜론과 endif를 이용할 수도 있다.

if ( condition ):
  statement;
endif;

예제

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>Coding Factory</title>
    <style>
      p {
        font-family: "Consolas", monospace;
        font-style: italic;
        font-size: 1.3em;
      }
    </style>
  </head>
  <body>
    <?php
      $var = 15;
      if ( $var > 10 ) {
        echo "<p>var is greater than 10.</p>";
      }
    ?>
  </body>
</html>
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>Coding Factory</title>
    <style>
      p {
        font-family: "Consolas", monospace;
        font-style: italic;
        font-size: 1.3em;
      }
    </style>
  </head>
  <body>
    <?php
      $var = 15;
      if ( $var > 10 ) :
    ?>
      <p>var is greater than 10.</p>
    <?php
      endif;
    ?>
  </body>
</html>

if, else

설명

if로 조건을 만족할 때 실행할 작업을, else로 조건을 만족하지 않을 때 실행할 작업을 정한다.

문법

if ( condition ) {
  statement1;
} else {
  statement2;
}

condition이 TRUE이면 statement1을 실행하고, FALSE이면 statement2를 실행한다.

중괄호 대신 콜론과 endif를 사용할 수도 있다.

if ( condition ):
  statement1;
else:
  statement2;
endif;

예제

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>Coding Factory</title>
    <style>
      p {
        font-family: "Consolas", monospace;
        font-style: italic;
        font-size: 1.3em;
      }
    </style>
  </head>
  <body>
    <?php
      $var1 = 15;
      $var2 = 5;
      if ( $var1 > 10 ) {
        echo "<p>var1 is greater than 10.</p>";
      } else {
        echo "<p>var1 is not greater than 10.</p>";
      }
      if ( $var2 > 10 ) {
        echo "<p>var2 is greater than 10.</p>";
      } else {
        echo "<p>var2 is not greater than 10.</p>";
      }
    ?>
  </body>
</html>
<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>Coding Factory</title>
    <style>
      p {
        font-family: "Consolas", monospace;
        font-style: italic;
        font-size: 1.3em;
      }
    </style>
  </head>
  <body>
    <?php
      $var1 = 15;
      $var2 = 5;
      if ( $var1 > 10 ) :
    ?>
    <p>var1 is greater than 10.</p>
    <?php
      else :
    ?>
    <p>var1 is not greater than 10.</p>
    <?php
      endif;
      if ( $var2 > 10 ) :
    ?>
    <p>var2 is greater than 10.</p>
    <?php
      else :
    ?>
    <p>var2 is not greater than 10.</p>
    <?php
      endif;
    ?>
  </body>
</html>

if, elseif, else

설명

if와 elseif로 여러 조건을 만든 후 각 조건에 만족할 때 실행할 작업을 정한다. 모든 조건을 만족하지 않을 때 실행할 작업은 else로 정한다.

문법

if ( condition1 ) {
  statement1;
} elseif ( condition2 ) {
  statement2;
} else {
  statement3;
}
  1. condition1이 TRUE이면 statement1을 실행하고 조건문을 빠져나온다.
  2. condition1이 FALSE이고 condition2가 TRUE이면 statement2를 실행하고 조건문을 빠져나온다.
  3. condition1과 condition2가 모두 FALSE이면 statement3을 실행한다.

elseif는 여러 번 사용할 수 있으며, else는 필수가 아니므로 사용하지 않아도 된다.

중괄호 대신 콜론과 endif를 사용할 수도 있다.

if ( condition1 ):
  statement1;
elseif ( condition2 ):
  statement2;
else:
  statement2;
endif;

예제 1

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>Coding Factory</title>
    <style>
      p {
        font-family: "Consolas", monospace;
        font-style: italic;
        font-size: 1.3em;
      }
    </style>
  </head>
  <body>
    <?php
      $var = 25;
      if ( $var > 10 ) {
        echo "<p>var is greater than 10.</p>";
      } elseif ( $var > 20 ) {
        echo "<p>var is greater than 20.</p>";
      } elseif ( $var > 30 ) {
        echo "<p>var is greater than 30.</p>";
      } else {
        echo "<p>var is not greater than 10.</p>";
      }
    ?>
  </body>
</html>

예제 2

<!doctype html>
<html lang="ko">
  <head>
    <meta charset="utf-8">
    <title>Coding Factory</title>
    <style>
      p {
        font-family: "Consolas", monospace;
        font-style: italic;
        font-size: 1.3em;
      }
    </style>
  </head>
  <body>
    <?php
      $var = 25;
      if ( $var < 10 ) {
        echo "<p>var is less than 10.</p>";
      } elseif ( $var > 10 and $var < 20 ) {
        echo "<p>var is greater than 10 and less than 20.</p>";
      } elseif ( $var > 20 and $var < 30 ) {
        echo "<p>var is greater than 20 and less than 30.</p>";
      }
    ?>
  </body>
</html>

같은 카테고리 다른 글

PHP / Tutorial / 접속한 IP 확인하는 방법

웹 개발을 하다 보면, 사용자 또는 클라이언트의 IP 주소를 확인해야 할 일이 종종 있습니다. 로그인 시도의 IP 로그 방문자 통계 분석 보안 접속 제한 PHP에서 접속한 IP는 다음 코드로 확인할 수 있습니다.

PHP / Tutorial / 연산자 / 비교 연산자

PHP의 비교 연산자는 두 값을 비교하여 조건의 참(TRUE) 또는 거짓(FALSE)을 반환할 때 사용합니다.

PHP / Tutorial / 반복문 / while, do-while, for

PHP / Tutorial / 반복문 / while, do-while, for

PHP에서 반복문은 코드를 여러 번 실행해야 할 때 유용합니다. PHP에는 여러 종류의 반복문이 있으며, 각기 다른 상황에서 사용할 수 있습니다.

PHP / Tutorial / 조건문 / if, elseif, else

PHP / Tutorial / 조건문 / if, elseif, else

PHP의 if 조건문은 코드의 특정 부분을 조건에 따라 실행할 수 있도록 하는 제어문입니다. 조건이 true로 평가되면 지정된 코드 블록이 실행됩니다.

PHP / Tutorial / 조건문 / switch

PHP의 switch문은 여러 조건을 평가해야 할 때 유용한 조건문입니다. if-else문과 비슷하지만, 특정 값과의 일치를 기준으로 실행 흐름을 제어하는 데 더 간결한 방법을 제공합니다.