PHP While and Do While Loops

In Programming, Looping enables you to run a group of statements for the specified number of times. These loops, in general executes the block of code repeatedly, till the condition remains true and stops them, when it becomes false.

PHP Loops

You often want the same block of code to run over and over again in a row when you write the code. Rather than adding several nearly equal code-lines to a script, we can use loops to execute such a task.

In PHP, we have the following looping statements:

  • while – loops through a block of code as long as the specified condition is true
  • do…while – loops through a block of code once, and then repeats the loop as long as the specified condition is true
  • for – loops through a block of code a specified number of times
  • foreach – loops through a block of code for each element in an array

The basic structure of loops is ‘While Loop’. The meaning is that ‘while a condition is true run some codes’.

The expression becomes true, the loop body is executed and then the expression is reassessed (if true, the loop group is executed). This process is exchanged from the loop until the test condition becomes false.

Syntax on PHP While loop:

You can see the structure below.

while (condition) 
{
code to be executed;
}

Example:

<?php

$b = 1;

while ($b<=5){

          $b++;

          echo (2*$b-1).'';

}

echo (2*$b-1);

?>

The answer is 3, 7, 9, 11 and again 11 in separated lines. The loop begins with 1 and the condition is checked (1<=5); because of true result, php goes to next line; add 1 to $a, so it becomes 2. At the next line 3 would be printed by the formula. Now the condition is checked again (2<=5) and it goes until the $a becomes 5. The condition checked again (5<=5); $a becomes 6; 11 would be printed. The condition is not true anymore; so PHP jumps from the loop and goes to the next of it; because of last value of $a, again 11 would be printed by the final line.

Alternative form of loop also exists as if block. It uses colon instead of bracket and ‘endwhile‘ to specify the end of the loop. So previous example changed to following example as an instance:

<?php
$b = 1;

while ($b<=5):

          $b++;

          echo (2*$b-1).'';

endwhile;

echo (2*$b-1).'';
?>

One line loops doesn’t’ need any brackets, colons or ‘endwhile’s:

<?php
$b = 1;

while ($b<=5)

          echo ++$b;
?>

Here we have ‘23456’ for result. ‘++’ adds one to $a first, and the new value will be sent to echo function then.

The PHP do…while Loop

This loop structure is like a while loop but the major  difference is that, the condition is checked at the end of each iteration. So, for the first time, the loop codes are run without any limits, and they’re done for all do while loops. But with ‘while‘ structure you may miss the loop at all, because if the condition at the top becomes false,  PHP code will jump out of the loop. Below example illustrates the concept.

The do…while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.

Syntax:

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

The example below first sets a variable $x to 1 ($y = 1). Then, the do while loop will write some output, and then increment the variable $y with 1. Then the condition is checked (is $y less than, or equal to 8?), and the loop will continue to run as long as $y is less than, or equal to 8:

<?php
$y = 1;
do {
echo "The number is: $y";
$y++;
} 
while ($y <= 8);
?>
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8

Note that in a do while loop the condition is tested AFTER executing the statements within the loop. This means, the do while loop would execute its statements at least once, even if the condition is false the first time.

Add a Comment

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

ABOUT CODINGACE

My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start codingace.com is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib

CEO: codingace.com

Request a Quote









PHP Code Snippets Powered By : XYZScripts.com