linuxStat PHP Documentation

Conditional execution of PHP code

The PHP if-elseif-else statement provides a mechanism
to test a chain of conditions in sequence and execute
a block of PHP code corresponding to the first condition
in the chain that is TRUE.

The if-elseif-else statement has the following structure:

if (condition1) {
   PHP Code 1
}
elseif (condition2) {
   PHP Code 2
}
elseif (condition3) {
   PHP Code 3
}
else {
   PHP Code 4
}

The PHP code interpreter tests condition1 first.
If condition1 is TRUE then PHP Code 1 is executed.
PHP Code 2, PHP Code 3, PHP Code 4 are not executed.
In addition, condition2 and condition3 are not tested.

If condition1 is FALSE then the PHP code interpreter
does not execute PHP Code 1. It then tests condition2.
If condition2 is TRUE then PHP Code 2 is executed.
PHP Code 3, and PHP Code 4 are not executed.
In addition, condition3 is not tested.

If condition2 is FALSE then the PHP code interpreter
does not execute PHP Code 2. It then tests condition3.
If condition3 is TRUE then PHP Code 3 is executed.
PHP Code 4 is not executed.

If condition3 is FALSE then the PHP code interpreter
does not execute PHP Code 3. PHP Code 4 is executed.


Example 9.1:
$age = 6;
if ($age <= 12) {    // Line 1
   print "Marvin K. Mooney, you are a preteen. <br>"; // Line 2
}
elseif ($age <= 19) {
   print "Marvin K. Mooney, you are a teenager. <br>"; // Line 3
}
else {
   print "Marvin K. Mooney, you are an adult. <br>"; // Line 4
}

Since the condition in Line 1 is TRUE, the print statement in Line 2
is executed. The print statement in Line 3 and the print statement
in Line 4 are not executed.

The PHP program in Example 9.1 embedded in an HTML document:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<title>Executed on Dell Server, Intel Processor, created on HP laptop</title>
</head>
<body bgcolor="#0F243E" text="#FFFF00" lang=EN-US>
<?php
# Example of how PHP if-elseif-else statement works.
$age = 6;
if ($age <= 12) {    // Line 1
   print "Marvin K. Mooney, you are a preteen. <br>";    // Line 2
}
elseif ($age <= 19) {    // Line 3
   print "Marvin K. Mooney, you are a teenager. <br>";    // Line 4
}
else {
   print "Marvin K. Mooney, you are an adult. <br>";    // Line 5
}
?>
</body>
</html>

Click the following link to execute the above PHP program
by my server's PHP interpreter.

Try the above example


Example 9.2:
$age = 15;
if ($age <= 12) {    // Line 1
   print "Marvin K. Mooney, you are a preteen. <br>";    // Line 2
}
elseif ($age <= 19) {    // Line 3
   print "Marvin K. Mooney, you are a teenager. <br>";    // Line 4
}
else {
   print "Marvin K. Mooney, you are an adult. <br>";    // Line 5
}

Since the condition in Line 1 is FALSE, the print statement in Line 2
is not executed. The condition in Line 3 is evaluated.
Since the condition in Line 3 is TRUE, the print statement
in Line 4 is executed. The print statement in Line 5 is not executed.

The program in Example 9.2 in an HTML document:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<title>Executed on Dell Server, Intel Processor, created on HP laptop</title>
</head>
<body bgcolor="#0F243E" text="#FFFF00" lang=EN-US>
<?php
# Example of how PHP if-elseif-else statement works.
$age = 15;
if ($age <= 12) {
   print "Marvin K. Mooney, you are a preteen. <br>";
}
elseif ($age <= 19) {
   print "Marvin K. Mooney, you are a teenager. <br>";
}
else {
   print "Marvin K. Mooney, you are an adult. <br>";
}
?>
</body>
</html>

Click the following link to execute the above PHP code
by my server's PHP interpreter.

Try the above example


Example 9.3:
$age = 21;
if ($age <= 12) {   // Line 1
   print "Marvin K. Mooney, you are a preteen. <br>"; // Line 2
}
elseif ($age <= 19) { // Line 3
   print "Marvin K. Mooney, you are a teenager. <br>"; // Line 4
}
else {
   print "Marvin K. Mooney, you are an adult. <br>"; // Line 5
}

Since the condition in Line 1 is FALSE, the print statement in Line 2
is not executed. The condition in Line 3 is evaluated.
Since the condition in Line 3 is FALSE, the print statement
in Line 4 is not executed. The print statement in Line 5 is executed.

The program in Example 9.3 in an HTML document:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/transitional.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<title>Executed on Dell Server, Intel Processor, created on HP laptop</title>
</head>
<body bgcolor="#0F243E" text="#FFFF00" lang=EN-US>
<?php
# Example of how PHP if-elseif-else statement works.
$age = 21;
if ($age <= 12) {
   print "Marvin K. Mooney, you are a preteen. <br>";
}
elseif ($age <= 19) {
   print "Marvin K. Mooney, you are a teenager. <br>";
}
else {
   print "Marvin K. Mooney, you are an adult. <br>";
}
?>
</body>
</html>

Click the following link to execute the above PHP code
by my server's PHP interpreter.

Try the above example    Next    Index