PHP while loop

PHP while is an iterative loop by which we can repeat a condition based on condition. The general form of PHP while loop is
while(condition)

{

executed satement

}

PHP while loop starts with the name while and then condition. If the condition is true then it allows to execute the statement otherwise it terminates the while loop. So we can say that the condition part returns true or false.

PHP while loop example

Now we try to do a problem using php while loop. The problem is to add 1,2,3,4,5,6,7,8,9 and 10. So the code is
<?php
$value=1;
$sum=0;
while($value<=10)
{
    $sum=$sum+$value;
    $value++;
    }
    echo $sum;
?>
If the while condition is not true then the PHP while loop will not execute the inner code. Here is an example.
<?php
$value=1;
$sum=0;
while(!$value==1)
{
    $sum=$sum+$value;
    $value++;
    }
    echo $sum;
?>

Comments

Popular posts from this blog

How to construct a B+ tree with example

How to show only month and year fields in android Date-picker?

Visitor Counter Script Using PHP