Control Statements In PHP

Introduction

 
Control statements 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.
 
Types
 
The If statement
The ? Operator
The switch statement
Loops
exit, die and return, exceptions
Declare
 
The If statement:
  1. if(expression1)  
  2. {  
  3.   Only exceutes when the ic condition is correct.  
  4. }  
  5. elseif(expression2)  
  6. {  
  7.   Executed when the if expression1  
  8.   is false and the expression 2 is true.  
  9. }  
  10. else  
  11. {  
  12.   Executed only when the both if block are false.  
  13. }  
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:
  1. <?php  
  2.          if(date("D") == "Tue")  
  3.         {  
  4.              print("Hello");  
  5.         }  
  6. ?>  

? 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.
  1. 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.
 

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.
  1. <?php  
  2.   $day = date(" l ");  
  3.   switch($day)  
  4.   {   
  5.    case "monday":   
  6.        print($day);  
  7.        break;  
  8.   case "tuesday"  
  9.       print($day);  
  10.       break;  
  11.   case "wednesday":  
  12.      print($day);  
  13.      break;  
  14.   case "thursday":  
  15.      print($day);  
  16.      break;  
  17.   case "friday":  
  18.      print($day);  
  19.      break;  
  20.   case "saturday":  
  21.      print($day);  
  22.      break;  
  23. default:  
  24.   print($day);  
  25. }?>  
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.
 

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,
  1. <?php  
  2. for($a = 1; $a < = 5; $a++)  
  3. {  
  4.   print("value of a is $a<br>\n");  
  5. }  
  6. ?>  
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.
 

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.
  1. <?php  
  2.  while (TRUE)  
  3.  {  
  4.     print("While loop is executed");  
  5.  }  
  6.  ?>  
The statement inside the while loop is executed when the condition is met else it skips the entire loop.
 

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.
  1. <?php  
  2. $a=10;  
  3. do  
  4. {  
  5.  print($a<br>\n)  
  6.  a=a+a;  
  7. while (a < 50);  
  8. ?>  
Output
 
10
20
40
 

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.
  1. foreach (array as key = > value)  
  2. {  
  3.   statement  
  4. }