PHP

Control Statements in PHP

In this article on Control Statements in PHP, I will cover conditional control statements and iteration control statements.

Understanding Control Statements in PHP

In fact, a control statement in a programming language alters the normal sequence of execution of statements in a program. Normally, a language compiler executes the statements of a program in the order in which the statements appear. However, this normal sequence of execution can be changed on specific conditions. Hence, the purpose of control statements is to check for certain conditions. Accordingly, a specific set of statements can be executed.

Conditional Control Statements in PHP

The following statements are used in PHP for decision making.

if statement

Syntax

if (condition){
  //statements
}

Example

$a=20;
if($a % 2 == 0){
   echo 'Even Number';
}
echo 'END';

In the above code, the condition in the if statement finds out whether the variable a is divisible by 2 or not. When the condition is true, the statement within if block executes. Otherwise, the control goes out of the if block. In any case, the last statement executes.

if…else statement

Syntax

if(condition){
  //statements
}
else{
  //statements
}

Example



$a=20;
if($a % 2 == 0){
   echo 'Even Number';
}
else{
   echo 'Odd Number';
}
echo 'END';

As can be seen in the above code, the else block is also present. When the condition in the if statement is true, the statements within the if block execute. Otherwise, the statements within the else block execute. After that, control comes out of the if…else statement and the statements following it execute.

if….elseif….else statement

Whenever, we have more than two conditions to evaluate, we can use this statement.

Syntax

if(condition){
   //statements
}
elseif(condition){
   //statements
}
elseif(condition){
  //statements
}
......
......
......
else{
   //statements
}

Example

The following code shows an example of using if…elseif…else statement. The variable usertype is assigned a value from the POST variable from an HTML form. If its value is ‘Admin’, the corresponding statement in the if block executes. Otherwise, the next condition is checked. It continues till a condition is satisfied or the else block encountered. In that case, the statements within the else block execute.

$usertype=$_POST['usertype'];
if($usertype=='Admin')
{
   echo 'Admin Dashboard';
}
elseif($usertype=='Customer')
{
    echo 'Customer Dashboard';
}
elseif($usertype=='Supplier')
{
    echo 'Supplier Dashboard';
}
elseif($usertype=='Operator')
{
    echo 'Operator Dashboard';
}
else
{
    echo 'Wrong User Type';
}

The Conditional Control Statements in PHP also include a switch statement. For more details on the switch statement click here.

Iteration Control Statements in PHP

The following list describes iteration control statements in PHP.

  • for statement
  • while statement
  • do…while statement
  • foreach statement

In order to find more details on Iteration Control Statements in PHP, click here.


Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

You may also like...

Leave a Reply

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