unknown os platform
Free Beginner PHP Tutorials - Do While loop | Whats-online.info
whats-online-info

Free Beginner PHP Tutorials - Do While loop


Free
2016/08/26 / Science and Tutorials

PHP Do while loop is a structure which tells a computer to execute a block of code while the specified condition is true. See example:-





PHP do while loop


 In a do..while loop, the condition is not evaluated until the end of each loop. That means, do...while loop will execute the block of code once, then check the condition and repeat the PHP loop as long as the specified condition is true.

Syntax

do {     execute code; } while (condition is true);

In do...while loop, the statement while() is closed by a semicolon unlike while loop.

Example

<?php  $sum=0; $x = 1;  do {              $sum=$sum + $x;       $x++; }while($x <= 10);  echo "The sum of first ten numbers is ".$sum; ?>

Results: The sum of first ten numbers is 55.

The example above finds the sum of the first ten numbers. First it initializes the sum to zero and sets a variable $x to 1 ($x = 1). Then, the do-while loop will get the initial sum and then the sum of increment of the variable $x with 1. Then the condition is checked if  $x is less than, or equal to 10?, The sum is calculated by getting the initial sum then adding the next number each time the loop will continue to run as long as $x is less than, or equal to 10.

 











You may also like:


Leave a comment












Subscribe to get replies direct to your mailbox

Submit comment