Control Statements In PHP

Introduction

Control statements in PHP are conditional statements that execute a block of statements if the condition is correct. The statement inside the conditional block will not execute until the condition is satisfied.

There are two types of control statements in PHP:

  • Conditional statements are used to execute code based on a condition. For example, you could use an if statement to check if a user is logged in, and then execute different code depending on whether they are or not.
  • Loop statements are used to repeat a block of code a certain number of times. For example, you could use a while loop to keep asking a user for their input until they enter a valid value.

Types

  • The If statement
  • The ? Operator
  • The switch statement
  • Loops
  • exit, die and return, exceptions
  • Declare

PHP If statement

if(expression1)  
{  
  Only exceutes when the ic condition is correct.  
}  
elseif(expression2)  
{  
  Executed when the if expression1  
  is false and the expression 2 is true.  
}  
else  
{  
  Executed only when the both if block are false.  
}

The if statement executes a statement if the expression inside the parenthesis is evaluated to true, or else the code is skipped to the next block. It may be a single statement followed by a semicolon and it is a compound statement surrounded by a curly braces. An else statement may appear immediately after the statement and have a statement of its own. It is executed only when the previous expression is false or else it is not executed.

A simple if statement:

<?php  
         if(date("D") == "Tue")  
        {  
             print("Hello");  
        }  
?>

PHP ? operator

It is represented as a ternary operator and it is used as a conditional operator. It is mainly evaluated to either false or true. If false the expression next to the ternary operator is executed or else expression between the ternary operator and colon is executed.

condition expresion ? true : false;  

It is mainly used to reduce the size of the code or else if can be used to reduce the complexity.

PHP Switch statement

Switch has many expressions and the condition is checked with each expression inside the switch. There is a default statement in the switch which can be used in the else statement and both have the same functionality and execute in the same way. A case is a beginning part for execution.

<?php  
  $day = date(" l ");  
  switch($day)  
  {   
   case "monday":   
       print($day);  
       break;  
  case "tuesday"  
      print($day);  
      break;  
  case "wednesday":  
     print($day);  
     break;  
  case "thursday":  
     print($day);  
     break;  
  case "friday":  
     print($day);  
     break;  
  case "saturday":  
     print($day);  
     break;  
default:  
  print($day);  
}?>

Break is also a control statement used to break the execution of the following statement. In switch it is able to compare two strings and execute the required statement.

When a case statement is executed for the required condition the remaining case statements are skipped only when the break statement is used or else all the case statements are executed in the switch statement.

PHP Loops

Loops are mainly used to repeat the process untill the conditions are met. Until the conditions are  met the loops repeatedly execute. If the the condition is not met, the loop will execute an infinite number of  times.

For loop,

<?php  
for($a = 1; $a < = 5; $a++)  
{  
  print("value of a is $a<br>\n");  
}  
?>

Output

value of a is 1

value of a is 2

value of a is 3

value of a is 4

value of a is 5

The loop is executed 5 times beacuse the condition is not satisfied in the next step so it exits the loop and stops repeating the execution.

PHP While Loop

It is the simplest form of looping statement. It checks the expression, and if true it executse the statement or else skips the entire code. It is mainly used when the value is exactly known. 

<?php  
 while (TRUE)  
 {  
    print("While loop is executed");  
 }
?> 

The statement inside the while loop is executed when the condition is met else it skips the entire loop.

PHP Do-while loop

This loop is executed once even if the condition is not met. After executing once it will check for conditions and execute the statement until conditions are met.

<?php  
$a=10;  
do  
{  
 print($a<br>\n)  
 a=a+a;  
} while (a < 50);  
?>

Output

10

20

40

PHP For Each statement

It provides a formalized method for iterating over arrays. An array is a collection of values referenced by keys. The for each statement requires an array and a definition of the variable to receive each element.

foreach (array as key = > value)  
{  
  statement  
}


Similar Articles