Control Statements in PHP with Examples

Posted on

The if, if…else and if…elseif…else construct are one of the most important features of many languages. These conditional statements provides us different actions for the different conditions. When we write code, we perform different actions for different decisions. Like any other languages, PHP is built out of a series of control statements. The control statement can be an assignment, a function call, a loop, a conditional statement or even a statement that does nothing or an empty statement.

In PHP we have the following conditional statements:

if statement – We use this control statement to execute some code only if a specified condition is true.

if…else statement – We use this control statement to execute some code if a condition is true and another code if the condition is false.

if…elseif….else statement – We use this control statement to select one of several blocks of code to be executed

switch statement – We use this control statement to select one of many blocks of code to be executed

1. The if Statement

Use the if statement to execute some code only if a specified condition is true.
The expression is evaluated to its Boolean value. If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE – it’ll ignore it

Syntax
if (condition) {
code to be executed if condition is true;
}

The following example would display ” A is bigger than B” if $a is bigger than $b:
<?php
if ($a > $b)
echo "A is bigger than B";
?>

2. The if…else Statement

elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE.

if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;

For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:
<?php
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";
}
?>

3. The if…elseif….else Statement

Use the if….elseif…else statement to select one of several blocks of code to be executed.

if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;

Note: Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.

4. The Switch Statement

The switch statement is similar to IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.

switch ( )
{
case condition1
break;
case condition2
break;
}

For example, the following code would display $i matched value as 0 or 1 or 2:
<?php
switch ($i) {
case 0:
echo "i equals 0";
case 1:
echo "i equals 1";
case 2:
echo "i equals 2";
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *