Control Statements In PHP – Free PHP Tutorials

Control Statement Basic Concepts

Control Statements bring condition based execution concept to the program. Many times it happens that we need to execute a certain part of the program according to some condition. In order enable the program for decision making we use control statements.

The various control statements available in PHP are as follows:

  1. if-else
  2. else-if-else
  3. switch

If-Else Control Statement

In this control statement, we give a certain condition in if block and the corresponding code that must be executed as the body of the if block. If the condition is true then the code gets executed. Otherwise, if it is false then the code inside the body of else gets executed.

The syntax is given below for implementation:

if(condition)

{

//body

}

else

{

//body

}

Look at the following example for more clarity:

<?php

$x=10;

if($x%2==0)//expression check

{echo "even";} //if condiotion is true print the statement

else

{echo "odd";} //if condition is not true print this statement

?>

Note: There is a format that can also be used and that is nested if. But that technique is hardly used because it is very confusing and inefficient as compared to others.


Else-If-Else Control Statement

This control statement is used when there is more than one condition. In that case, we must have a mechanism where the program must reach the correct specified condition. For such problems, we implement this concept. If a condition is true then the corresponding code gets executed else it moves forward and checks for the next condition.

The syntax is as follows for implementation:

if(condition)

{  
//body
 }

else if(condition)

{
 //body
 }

.

.

else

{
//body
 }

Look at the following example for more clarity:

<?php
$n=8;
if($n==10) //first check condition
{echo "Grade A";} // corresponding execution statement
else if($n<10 && $n>=7)//second check condition
{echo "Grade B";}//corresponding execution statement
else if($n<7 && $n>=5)//third check condition
{echo"Grade C";}//corresponding execution statement
else if($n<5 && $n>=2)//fourth check
{echo "Grade E";}//corresponding execution statement
else
{echo "FAIL";}// if nothing executes then execute this statement
?>

Switch Control Statement in PHP

This control statement  comes in handy when only want to execute only a particular block of code for a given value. This method if much efficient than the previous one. If none of the value matches the specified one, then a default statement gets executed.

Look at the syntax of the switch case and how it is implemented:

switch(value)

{
case 1: 
//body
break;

case 2;
//body
break;

.

.

.

default :
//body

}

Look at the following example for more clarity:

<?php

$x=5;
switch($x)
{
case 1: echo " number is 1"; //first case body
break;
case 2: echo"number is 2"; //second case body
break;
case 3: echo"number is 3";//third case body
break;
case 4: echo"number is 4";//fourth case body
break;
case 5: echo"number is 5";//fifth case body
break;
default: echo"no number selected"; //if all cases fail, then execute the default statement
}
?>

As you can see above that for each different values of $x we have a separate block to execute. There is a default statement given at the when none of the value matches.

We can conclude by saying that the above-mentioned control structures are ideal for using it to control the execution of the program. If-else is used when we have one route block, for more route block we have else-if else. When we have various different blocks,  switch case is used.

Leave a Comment

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