Monday, 20 May 2013

loops in php


PHP loops:

    In programming it is often necessary to repeat the same block of code a given number of times, or until a certain condition is met. This can be accomplished using looping statements.

    PHP has two major groups of looping statements:
  • for
  • and while.


  • for :
    The For statements are best used when you want to perform a loop a specific number of times. 

    while :
    The While statements are best used to perform a loop an undetermined number of times. In addition, you can use the Break and Continue statements within looping statements.
    PHP loops are control structures and you can use them the execute a code block more times. It means you don't have to copy and paste your code many times in the file just use a right loop statement.

    In php we use four types loos those are describe below:
  • 1-while loop
  • 2-do ... while loop
  • 3-for loop
  • 4-foreach loop

PHP for Loop:

    PHP for loops allow you to execute the same piece of code for a specified number of times. Once the specified number has been reached, the program will break out of the loop and continue processing the rest of the page.
    The common tasks that are covered by a for loop are:
  • Set a counter variable to some initial value.
  • Check to see if the conditional statement is true.
  • Execute the code within the loop.
  • Increment a counter at the end of each iteration through the loop.

  • Syntax
    A PHP for loop requires 3 parameters.
  • The 1st parameter sets a counter
  • 2nd parameter defines how high the counter should get before breaking from the loop
  • 3rd parameter is used to increment the counter.
  • for (initialization;condition; increment/decreament) 



    Code to be executed while the counter is less than the specified count; 

    }


    Example

    <?php 

    for ($xint=0; $xint<=5; $xint++) 



    echo $xint; 



    ?>
    Output
        012345

    Example

    <?php $price = 5; echo "&lt;table border="1" align="center"&gt;"; echo "&lt;tr&gt;&lt;th&gt;No of items&lt;/th&gt;"; echo "&lt;th&gt;Price&lt;/th&gt;&lt;/tr&gt;"; for($a=10;$a<=100;$a+=10) 
    {
    echo "&lt;tr&gt;&lt;td&gt;"; echo $a; echo "&lt;/td&gt;&lt;td&gt;"; echo $price * $a; echo "&lt;/td&gt;&lt;/tr&gt;"; } echo "&lt;/table&gt;";

    ?>
    Output
    No of itemsPrice
    1050
    20100
    30150
    40200
    50250
    60300
    70350
    80400
    90450
    100500

    PHP while Loop:

      PHP while loops allow you to execute the same piece of code continuously while a certain condition is true. Once the condition becomes false, the program will break out of the loop and continue processing the rest of the page.
      Repetitive tasks are always a difficult to us as
    • Deleting spam email
    • sealing 50 envelopes

    • and going to work are all examples of tasks that are repeated. The nice thing about programming is that you can avoid such repetitive tasks with a little bit of extra thinking. Most often these repetitive tasks are conquered in the loop.

      Syntax
      initialization; for (condition) { Code to be executed while 
      the counter is less than the specified count; increment/decreament; }


      Example

      <?php $i=1; while ($i&lt;=9) { echo " $i "; $i++; } ?>
      Output
          123456789

      Example

      <?php $price = 5; $item = 10; echo "&lt;table border='1' align='center'&gt;"; echo "&lt;tr&gt;&lt;th&gt;No of items&lt;/th&gt;"; echo "&lt;th&gt;Price&lt;/th&gt;&lt;/tr&gt;"; while ( $item &lt;= 100 ) { echo "&lt;tr&gt;&lt;td&gt;"; echo $item; echo "&lt;/td&gt;&lt;td&gt;"; echo $price * $item; echo "&lt;/td&gt;&lt;/tr&gt;"; $item = $item + 10; } echo "&lt;/table&gt;";

      ?>
      Output
      No of itemsPrice
      1050
      20100
      30150
      40200
      50250
      60300
      70350
      80400
      90450
      100500

    PHP do-while Loop:

      The do while construct consists of a process symbol and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again. This repeats until the condition becomes false.
      On the other hand, a do-while loop always executes its block of code at least once. This is because the conditional statement is not checked until after the contained code has been executed.

      Syntax
      do { code to be executed; } while (condition);


      Example

      <?php $cookies = 0; do { echo "ok bye!"; } while ($cookies &gt; 1); ?>
      Output
          ok bye!

No comments:

Post a Comment