Flow Control

In this lesson of the PHP tutorial, you will learn...
  1. To work with if-elseif-else conditions in PHP.
  2. To work with switch/case statements in PHP.
  3. To work with loops in PHP.

Conditional Processing

Conditional processing allows programmers to output different code based on specific conditions. There are two conditional structures in PHP - if-elseif-else and switch/case.

If Conditions

Simple if statement

Syntax
if (conditions)
 Do this;

In the above code, the Do this; statement will either run or not run depending on whether or not the conditions are true. This syntax can only be used when the condition affects a single line of code. For a block of code, use the following syntax.

Syntax
if (conditions)
{
 Do this;
 Then do this;
 And this too;
}

The lines of code affected by the if condition are put in a code block, which is surrounded by curly brackets to indicate that all of the code either should or should not be executed, depending on the result of the if condition.

if-else statement

Syntax
if (conditions)
{
 Do this;
}
else
{
 Do that;
}

if-elseif-else statement

Syntax
if (conditions)
{
 Do this;
}
elseif (other conditions)
{
 Do that;
}
else
{
 Do this other thing;
}

The two syntax blocks above show an if-else and an if-elseif-else statement, which can have any number of elseif blocks.

The following table shows PHP's comparison operators.

Comparison Operators
Operator Description
== Equals
!= Doesn't equal
> Is greater than
< Is less than
>= Is greater than or equal to
<= Is less than or equal to
=== Identical (same value and same type)
!== Not Identical

The following example demonstrates an if-elseif-else statement.

Code Sample: FlowControl/Demos/If.php

<html>
<head>
<title>if-elseif-else</title>
</head>
<body>
<?php
$Age = 21;
if ($Age >= 21) 
{
 echo 'You can vote and drink!';
} 
elseif ($Age >= 18)
{
 echo 'You can vote, but can\'t drink.';
} 
else 
{
 echo 'You cannot vote or drink.';
}
?>
</body>
</html>
Code Explanation

The file is relatively simple. You can see the different results by changing the value of $Age.

Compound If Statements

More complex if statements often require that several conditions be checked. The table below shows and and or operators for checking multiple conditions and the not operator for negating a boolean value (i.e, turning true to false or vice versa).

Logical Operators
Operator Name Example
&&
and
AND
$a && $b
||
or
OR
$a || $b
!
NOT
!$b

The following example shows these logical operators in practice.

Code Sample: FlowControl/Demos/If2.php

<html>
<head>
<title>if-elseif-else</title>
</head>
<body>
<?php
$Age = 21;
$Citizen = false;
if ($Age >= 21 && !$Citizen) 
{
 echo 'You can drink, but can\'t vote.';
} 
elseif ($Age >= 21) 
{
 echo 'You can vote and drink!';
} 
elseif ($Age >= 18 && $Citizen)
{
 echo 'You can vote, but can\'t drink.';
} 
else 
{
 echo 'You cannot vote or drink.';
}
?>
</body>
</html>

switch/case

A switch/case statement is similar to an if statement, except that it can only check for an equality comparison of a single expression. It cannot, for example, be used to check if one value is higher than another.

Syntax
switch (expression)
{
 case 'a' :
  echo 'expression is a';
  break;
 case 'b' :
  echo 'expression is b';
  break;
 case 'c' :
  echo 'expression is c';
  break;
 default :
  echo 'expression is unknown';
  break;
}

The break statement is important. Without it, after a single match is found, all following statements will execute.

The following example demonstrates a switch/case statement without break statements.

Code Sample: FlowControl/Demos/Switch.php

<html>
<head>
<title>switch/case</title>
</head>
<body>
<?php
$Quantity = 1;
switch ($Quantity)
{
 case 1 :
  echo 'Quantity is 1';
 case 2 :
  echo 'Quantity is 2';
 default :
  echo 'Quantity is not 1 or 2';
}
?>
</body>
</html>
Code Explanation

The screenshot below shows the result.

Notice that, once a match is found, all remaining echo statements are output. The following example shows how this can be fixed by adding break statements.

Code Sample: FlowControl/Demos/SwitchWithBreak.php

<html>
<head>
<title>switch/case</title>
</head>
<body>
<?php
$Quantity = 1;
switch ($Quantity)
{
 case 1 :
  echo 'Quantity is 1';
  break;
 case 2 :
  echo 'Quantity is 2';
  break;
 default :
  echo 'Quantity is not 1 or 2';
}
?>
</body>
</html>
Code Explanation

This time, only the first statement is output:

Loops

As the name implies, loops are used to loop (or iterate) over code blocks. The following section shows the syntax for different types of loops. Each loop will return "12345".

There are several types of loops in PHP.

  • while
  • do...while
  • for
  • foreach

while

while loops are used to execute a block of code repeatedly while one or more conditions is true.

Syntax
$a=1;
while ($a < 6)
{
 echo $a;
 $a++;
}

do...while

do...while loops are used to execute a block of code repeatedly until one or more conditions is found to be false. The difference between while loops and do...while loops is that the condition is checked after the code block is executed. This means that, in a do...while loop, the code block will always be executed at least once.

Syntax
$a=1;
do
{
 echo $a;
 $a++;
}
while ($a < 6);

for

A for loop takes three expressions separated by semi-colons and grouped in parentheses before the block to be iterated through.

  1. The first expression is executed once before the loop starts. It is usually used to initialize the conditions.
  2. The second expression is evaluated before each iteration through the loop. If it evaluates to false, the loop ends.
  3. The third expression is executed at the end of each iteration through the loop. It is usually used to make changes that can affect the second expression.
Syntax
for ($a=1; $a < 6; $a++)
{
 echo $a;
}

break and continue

To break out of a loop, insert a break statement.

Syntax
for ($a=1; $a < 6; $a++)
{
 echo $a;
 if ($a > 3)
 {
  break;
 }
}

To jump to the next iteration of a loop without executing the remaining statements in the block, insert a continue statement.

Syntax
for ($a=1; $a < 6; $a++)
{
 if ($a == 3)
 {
  continue;
 }
 echo $a;
}

Flow Control Conclusion

In this lesson of the PHP tutorial, you have learned to write if-elseif-else conditions, switch/case statements, and several different types of loops.

To continue to learn PHP go to the top of this page and click on the next lesson in this PHP Tutorial's Table of Contents.

Use of this website implies agreement to the following:

Copyright Information

All pages and graphics on this Web site are the property of Webucator, Inc. unless otherwise specified.

None of the content on this website may be redistributed or reproduced in any way, shape, or form without written permission from Webucator, Inc.

No Printing or saving of web pages

This content may not be printed or saved. It is for online use only.


Linking to this website

You may link to any of the pages on this website; however, you may not include the content in a frame or iframe without written permission from Webucator, Inc.


Warranties

This website is provided without warranty of any kind. There are no guarantees that use of the site will not be subject to interruptions. All direct or indirect risk related to use of the site is borne entirely by the user. All code and explanations provided on this site are provided without warranties to correctness, performance, fitness, merchantability, and/or any other warranty (whether expressed or implied).

For individual private use only

You agree not to use this online manual to deliver or receive training. If you are delivering or attending a class that is making use of this online manual, you are in violation of our terms of service. Please report any abuse to courseware@webucator.com. If you would like to deliver or receive training using this manual, please fill out the form at http://www.webucator.com/Contact.cfm.