Introduction to PHP – Part4

advertisement

advertisement

Control Statements

A program is a collection of modules working together to perform certain task. The modules can be thought independently which always produces the same set of output for a given set of input. Control statements control the execution of a program by executing those modules once at a time in a meaningful order,  known as flow of control. Basically, there are two types of control statements, branching statements and loops. In this tutorial, we will discuss branching statements in detail.

Branching statements

As the name suggest, the control of execution is passed to some code block among many code blocks depending upon the conditional statement. There are two branching statements available in PHP, Conditional Statement and Switch.

Conditional Statements

A conditional statement is used to decide which code block will be executed. Let us take an example of examining a number and printing it in word form, the conditional statement can be one of the following, depending upon the context.

if statement:  This statement is used to execute some code if a specified condition is true and does not care if the condition is false. In a situation where we need to print “One” if the value of x is 1, the code looks like

Example#1

$x=1;
if ($x==1)
       echo "One";

The output of example#1 will be “One”.

if…else statement: This statement is used to execute some code if a specified condition is true and another code if the condition is false. Let us modify the situation of example#1 by adding a statement that is executed if  the value of x is not 1, assume that it is an error.

Example#2

$x=0;
if ($x==1)
       echo "One";
else
       echo "Error...";

 

 The output of example#2 will be “Error…”.

if…elseif….else statement: This statement is used to select one of several blocks of code to be executed. Let us suppose another situation where we need to print “One” if the value of x is 1, “Two” if the value of x is 2, “Three” if the value x is 3 and error in other cases; the code looks like

Example#3

$x=3;
if ($x==1)
       echo "One";
else if($x==2)
       echo "Two";
else if($x==3)
       echo "Three";
else
       echo "Error...";

The output of example#3 will be “Three”.

Switch

Switch is functionally similar to if…elseif…else statement. In Switch, the conditional expressions in if…elseif statements are replaced by case and else is replaced by default. One more thing to understand in Switch is break. In example#4, the execution begins from case 1, the value of x is 2 and hence, it will not enter into case 1. It will enter into case 2 and will display the word “Two” and then switch execution will be break by using break statement. But if we don’t use break statement it will print “TwoThreeError…”. That means after a certain condition is satisfied, if we don’t break the execution, the Switch will continue executing the other cases even though the condition is not satisfied.

Example#4

$x=2;
switch($x)
{
case 1:
       echo "One";
       break;
case 2:
       echo "Two";
       break;
case 3:
       echo "Three";
       break;
default:
       echo "Error...";
}

About the Author:

Bhoj R Dhakal is a programmer. He has few years of experience in software development and maintenance. He has published series of PHP tutorials for webanddesigners.