PHP Control Structures

By | March 29, 2014

Introduction to Control Structures

When you start writing programs in PHP, you will realize the importance of using control structures. They are use to control how a structure will be executed, adding useful functionality to the PHP code and making it more flexible.

Condition and Statements

Control structure are usually based on condition and therefore always have condition associated with them .They check the status of these conditions ,which can be either true or false .A statement can be used with a control structure to tell the program what to do depending on the result of the condition specified. Consider the fallowing example to understand how a condition differs from a statement. If Rishi promises that he won’t neglect his studies, buy a Tab for him. This sentence can be broken intotwo parts, a condition and a statement. The first part of sentence, If Rishi promises that he won’t neglect his studies, is a condition. The status of the condition will decide whether Rishi  will get the Tab or not. If the condition is true , Rishi will get the Tab.

AS the name suggest, conditioal statement use to check for condition and execute code on the basis of the value returned. This section discuss if, else, elseif and switch conditional statements.

The if statement

We discussed the if statement briefly in the previous chapter; this section contains a detailedexplanation of it .The if statement is one of the most commonly use statement in PHP or any programming language.

Allows conditional execution.

The if statement can check for a particular condition and also perform a specific action depending on whether the condition is true or false.

In capable of containing multiple code blocks. You can set more than one operation to be performed depending on the result of the condition specified in the PHP code.

Can also be nested within an existing if statement. You can use another If statement within an exciting one to check another condition within an exciting condition.

$a = 5;
$b = 7;
$c = $a+$b;
  if ($c > 9){ 
    echo "The sum is greater than 9.";

The else statement

The else statement as discussed in the previous chapter is always used with the if statement. When you use the if statement you check condition and specify a statement to be executed when the condition is true. The else statement is used to specified a statement in case the result of the condition is false. The uses of else statement is similar to that of the if statement. The else statement is also used to check the alternative block of code.

$a = 10;
$b = 5;
$c = $a + $b;
 if ($c == 20) {
   echo "The sum is equal to 20.";
     echo "The sum is not equal to 20.";

The elseif statement

The elseif statement, as the name suggest, is a combination of the else and if  statements. you can check for multiple conditions by using the elseif statement. you can also have multiple elseif statement under an if statement. An else statement is executed only when the previous if statementreturns a false value. You can write both else if or elseif, with space and without space both are correct.

$playing = "Chess";
if ($playing == "Cricket") {
  echo "Rishi is playing cricket.";
}else if ($playing == "Chess"){
  echo "Rishi is playing Chess.";
  echo "Rishi is not playing.";

The switch statement

The switch statement can be used in placement of the if statement. Similar to the if statement, the switch statement is also used to change the flow of a program depending on the evolution of an expression. Although the if statement is also used to achieve the same result, there are certain differences between the if and switch statement:

– The else if statement can be used with the if statement to evaluate multiple conditions and expressions. In contrast switch evaluates only one expressions at a time and executes the corresponding code specified for the expressions.

– The result generated as part of the if statement returns a Boolean value, which is either true or false, but in the case of switch ,the result of a condition can be evaluated against any number of values.

$game = "Chess";
    case "Cricket";
       echo "Rishi is playing Cricket";
    case "Football";
       echo "Rishi is paying Foot Ball";
    case "Chess";
       echo "Rishi is playing Chess";

In this code notice that
– Multiple case statement have been used with respect to the switch statement.
– Each case statement ends with a colon.
– A break statement is include in each case statement.

The output of the code is: Rishi is playing Chess

Leave a Reply

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