Flow-Control Statements (Control Structures)

As you get deeper into programming, you will see a need of executing a series of statements based on conditions. Based on your requirement, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces like { and }.

if construct


if (expr)
    statement

If the expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it'll ignore it.

Let us look at a simple logical flow before we attempt to write PHP code for the same. In fact, when I started to learn programming, I first started to write simple english statements and then wrote coding for them. So, here it is:

Step1 - I want to check if value of $a is bigger than 5
Step2 - If value of $a is bigger than 5, I want to print "Bigger than 5" on screen. Else if value is not bigger than 5, I want to do nothing.
Step3 - End of my logic

Now, writing the same code, programmatically:

<?php
$a 
6;
if (
$a 5) {
   echo 
"Bigger than 5";
   }
?>

In the above code, the expression is evaluated to either TRUE or FALSE and in this case, if value of $a is found to be bigger than 5, it evaluates to TRUE and then the statements within the statement-group are then executed. In this example, we have already assigned a value of 6 to $a, but you should try to assign values less than 5 and test the results. As we said earlier, you can group more statememts in the statement-group and execute them. Try the example and check the result:

<?php
$a 
6;
if (
$a 5) {
   echo 
"I evaluated the value of a";
   echo 
"and found it to be";
   echo 
"bigger than 5";
   }
?>

You could also do the following:

<?php
$a 
6;
if (
$a 5) {
   echo 
"Bigger than 5";
   
$b $a;
   }   
?>

Now, if you were to execute the above code, it will print the string "Bigger than 5" and then assign the value of $a to $b. Have some fun with this code, try this:

<?php
$a 
6;
$b 10;
if (
$a 5) {
   echo 
"Bigger than 5";
   
$b $a;
   }
echo 
$b;
?>

Here, if $a is bigger than 5, value of $a is assigned to $b and the same is printed on screen. Change the value of $a in first line to 4 and see what happens?

I hope, you had a good learning session with if construct. If you need more examples in this section, please contact us and let us know.

You now know how to execute statement(s) if certain condition is met. In this section we will execute a statement in case the expression in the if statement evaluates to FALSE. Let's start with looking with the example that we had used earlier:

<?php
$a 
6;
$b 10;
if (
$a $b) {
   echo 
"a IS bigger than b";
   } else {
   echo 
"a is NOT bigger than b"
   
}
?>

In the above example, swap the values of $a and $b and then check the results. I hope, you are understanding the use of else construct. Often, we want to execute statement, when the expression in if statement is evaluated to TRUE. But, in case, we want to execute a different set of statement when the expression is evaluated to FALSE and that's when else plays a role. Remember that else statements are executed ONLY when the expression in if statement is evaluated to FALSE. There is another construct called 'elseif' which is a combination of if and else. Unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE. See this example elseif statement for better understanding:

<?php
$a 
1;
$b 2;
if (
$a $b) {
   echo 
"a is bigger than b";
} elseif (
$a == $b) {
   echo 
"a is equal to b";
} else {
   echo 
"a is smaller than b";
}
?>

In this example, the if construct evaluates if $a is bigger than $b. If TRUE, it will execute the statement within the curly brackets which is "a is bigger than b". If found to be FALSE, it will move onto elseif construct which will then evaluate the condition to check if values of $a and $b are equal (note the use of ==, Comparison Operators) and if found TRUE will execute the statement "a is equal to b". If FALSE, it will move onto the else construct which does not evaluate any condition but will execute the statement "a is smaller than b" because the condition of if previous expression was evaluated to be false. Try changing the values of $a and $b to understand this better.

while statement

Using while statement is very simple. Let's first look at the basic syntax/form of while statement:

while (expr)
   statement

In while statement, group of statement(s) are executed as long as the expression in while statement evaluates to TRUE. The condition and its evaluation is checked at the beginning of each iteration. A simpler example of while statement would be:

Step1 - Assign value of 1 to $i
Step2 - Print the value of $i and increment it by 1 till the value of $i becomes 10

Writing it using while statement would look something like:

<?php
$i 
1;
while (
$i <= 10) { //Execute the statements below if this is TRUE
   
echo $i//Print the value of $i
   
echo " "//print a space
   
$i++;  //Increment value of $i by 1
   
}
?>

do-while loop

Let us start with the syntax of do-while loops:

do {
   statement
} while (expr);

The only difference between a while and do-while statement is that expression is evaluated at the end of each iteration instead of doing it at the beginning. In a do-while loop, the statement will run the first time and when it reaches end of statements the expression is evaluated. Based on the exression being TRUE or FASLE, this iteration is either repeated or ends.
See this example of do-while loop:

<?php
$i 
1;
do {
   echo 
$i;
   echo 
" "//print a space
   
$i++;
} while (
$i >= 10);
?>

In the example, it first prints the value of $i, then increments it by 1 and then evaluates the expression in while statement. Try changing the value of $i from 1 to 10 and see what happens?

for loops

for loops are more complex in strcuture than other loop strutures in PHP. The general syntax for a for loop is:

for (expr1; expr2; expr3)
   statement

Here, the first expression (expr1) is executed without any conditions at the very beginning of the loop. Then at the beginning of each iteration, the second expression (expr2) is evaluated and if found to be TRUE, the nested statements are executed. This is one iteration cycle. If the expression (expr2) is evaluated to be false, the execution of the loop ends there. And, at the end of each iteration, the third expression (expr3) is executed.


In this example, the first expression is '$i=1' which is executed without any conditions. Now the iteration begins, and at the beginning of the iteration, the second expression $i less than or equal to 10 is evaluated. Since it evaluates to FALSE ($i is 1 and is smaller than 10), the statement 'echo $i' is executed and then the last expression '$i++' is executed thus incrementing the value of $i by 1. Now, we are inside the loop, and second expression is evaluated, is found false, prints the value of $i and increments by 1. This iteration continues till the second expression evaluates to be TRUE which is value $i being equal to 10.

foreach construct

PHP provides us with an easy way to iterate over arrays. Since we have not convered arrays so far, we will skip this construct here for now. You may however visit the PHP documentation site to read about foreach construct.

switch statement

One of the most easiest statement is switch statement. It matches the value of the variable in context and if the value matches, it then executes certain code depending on what you want it to execute. All switch statements evaluate every case with a comparision operator '=='. This might cause some probelm with comparing string to integers. Let's understand switch statement with an example:

<?php
switch ($input) {
case 
"a":
   echo 
"input is a";
   break;
case 
"b":
   echo 
"input is b";
   break;
case 
"c":
   echo 
"input is c";
   break;
   }
?>

Switch statements are not good only for strings but also holds equally good for numbers. See this example:

<?php
switch ($input) {
case 
0:
   echo 
"input is 0";
   break;
case 
1:
   echo 
"input is 1";
   break;
case 
2:
   echo 
"input is 2";
   break;
   }
?>

switch statements can also be empty, like this:

<?php
switch ($input) {
case 
0:  //This is empty
case 1:
   echo 
"input is 1";
   break;
case 
2:
   echo 
"input is 2";
   break;
   }
?>

When a case is not found, you may find it easier to handle with 'default':

<?php
switch ($input) {
case 
0:
   echo 
"input is 0";
   break;
case 
1:
   echo 
"input is 1";
   break;
case 
2:
   echo 
"input is 2";
   break;
default:
   echo 
"input is not 0, 1 or 2";   
   }
?>