Monday 28 November 2011

Conditional Structure and Looping Structure in PHP


Ø       Conditional Structure

Conditional statement are the statement which are used when we want to check any condition between two variable.

                        PHP provides us with some kind of conditional statement as below.

  • Simple If Statement

Generally this kind of statement are used when we want to check only the true condition. It will process only if the condition is true otherwise nothing.

          Syntax

                        if(condition)
                   {
                             Code to be executed if condition is true
                   }

Example

<?php
          $a=10;
          $b=20;
          if($a!=$b)
          {
                   echo "a and b are not same value";
          }
          ?>

  • If – Else Statement

Use this statement if you want to execute some code if the condition is true and another code if the condition is false.

          Syntax

                        if(condition)
                   {
                             Code to be executed if condition is true
                   }
                   else
                   {
                             Code to be executed if condition is false

                   }

Example

<?php
          $m1=40;
          $m2=60;
          $m3=50;
          if($m1>=40 && $m2>=40 && $m3>=40)
          {
                   echo "Result : Pass";
          }
          else
          {
                   echo "Result : Fail";
          }
          ?>

  • If – Else - if Statement

Use this statement if you want to select one of many blocks of code to be executed.

            Syntax

                        If(condition1)
                   {
                             Code to be executed if condition is true
                    }
                    else If(condition2)
                   {
                             Code to be executed if condition is true
                   }
                   else If(condition_N)
                   {
                             Code to be executed if condition is true
                   }
                   else
                   {
                             Code to be executed if condition is false
                   }

Example

<?php
          $str ="red";
          if($str == "red")
          {
                   echo "Stop";
          }
          else if($str == "yellow")
          {
                   echo "Wait";
          }
          else
          {
                   echo "Go";
          }
                   ?>

  • Switch Statement

You should use the switch statement if you want to select one of many blocks of code to be executed.

            Syntax

                        Switch(expression)
                   {
                             case 1:
                                      Execute code block 1
                                      break;
                             case 2:
                                      Execute code block 2
                                      break;
                             default:
                                      code to be executed if n is different from case1 and case 2;
                                      break;
                   }
                       
  • First we have a single expression n that is evaluated once
  • The value of expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed.
  • Use break to prevent the code from running into the next case automatically.

Example

<?php
          $a=1;
          switch($a)
          {
                   case 1:
                             echo "January";
                             break;
                   case 2:
                             echo "February";
                             break;
                   case 3:
                             echo "March";
                             break;
                   default:
                             echo "Try again";
                             break;
          }
                   ?>

Ø       Looping Structure

Very often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we can use loops to perform a task like this.

In PHP there are three different kind of loops.

1.for loop
2.while loop
3.do while loop

  • The for loop

Loops through a block of code a specified number of times. The for loop is used when you know in advance how many times the script should run.

          Syntax

                        for(initialization; condition; increment or decrement)
                   {
                             Code  to be executed
                   }

The for loop has three section initialization, condition, increment or decrement.

Initialization: means the variable is to be started with some values. This means starting value to be given to the variable.

Condition: condition section check the condition if the condition is true then code to be executed else executed the next statement after for loop.

Increment or decrement: to process the loop we have to either increment the variable or decrement the variable otherwise the loop will not process.

Example

<?php
          for($a=1;$a<=10;$a++)
          {
                   echo $a."<br/>";
          }
          ?>

  • The while loop

The while loop is used when you want the loop to execute and continue executing while the specified condition is true.

            The increment of the number is done manually inside the loop.

            Syntax

                        while(condition)
                   {
                             Code  to be executed
                             Increment or decrement of the variable
                   }

Example

<?php
          $a=1;
          while($a<=10)
          {
                   echo $a."<br/>";
                   $a=$a+1;
          }
          ?>

  • The do…while loop

The do while loop is a variant of the while loop. The loop will always execute a block of code  once, and then it will repeat the loop as long as the specified condition is true.

This loop always be executed once, even if the condition is false, because the code are executed before the condition is tested.

Syntax          

                   do
                   {
                             Code  to be executed
                   }
while(condition);

Example

<?php
          $a=1;
          do
          {
                   echo $a."<br/>";
                   $a=$a+1;
          }
          while($a<=10);
     ?>

  • foreach statement

          The foreach statement is used to look through arrays.

For every loop the value of the current array element is assign to $value and the array pointer is moved by 1, so on the next loop you will be looking at the next element.

            Syntax

                        foreach(array as value)
                   {
                             Code  to be executed
                   }

Example

<?php
          $arr=array("one","two","three");
          foreach($arr as $value)
          {
                   echo "value is ".$value."<br/>";
          }
          ?>

Output

             Value is one
           Value is two
           Value is three

  • foreach with key type value

                        The following example that will print the key and value of given array.

Example

<?php
          $arr=array("one","two","three");
          foreach($arr as $key=>$value)
          {
                   echo "key is $key & value is ".$value."<br/>";
          }
          ?>

Output

key is 0 & value is one
key is 1 & value is two
key is 2 & value is three

Posted By : Hemangi Zala

No comments:

Post a Comment