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;
}
- condition1이 TRUE이면 statement1을 실행하고 조건문을 빠져나온다.
- condition1이 FALSE이고 condition2가 TRUE이면 statement2를 실행하고 조건문을 빠져나온다.
- 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>





