How do you make a while loop in php?


The while loop - Loops through a block of code as long as the specified condition is true.


The PHP while Loop

The while loop executes a block of code as long as the specified condition is true.

Syntax

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

Examples

The example below displays the numbers from 1 to 5:

Example

$x = 1;

while($x <= 5) {
  echo "The number is: $x
";
  $x++;
}
?>

Try it Yourself »

Example Explained

  • $x = 1; - Initialize the loop counter ($x), and set the start value to 1
  • $x <= 5 - Continue the loop as long as $x is less than or equal to 5
  • $x++; - Increase the loop counter value by 1 for each iteration

This example counts to 100 by tens:

Example

$x = 0;

while($x <= 100) {
  echo "The number is: $x
";
  $x+=10;
}
?>

Try it Yourself »

Example Explained

  • $x = 0; - Initialize the loop counter ($x), and set the start value to 0
  • $x <= 100 - Continue the loop as long as $x is less than or equal to 100
  • $x+=10; - Increase the loop counter value by 10 for each iteration


PHP Exercises




The do...while loop - Loops through a block of code once, and then repeats the loop as long as the specified condition is true.


The PHP do...while Loop

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);

Examples

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

Example

$x = 1;

do {
  echo "The number is: $x
";
  $x++;
} while ($x <= 5);
?>

Try it Yourself »

Note: In a do...while loop the condition is tested AFTER executing the statements within the loop. This means that the do...while loop will execute its statements at least once, even if the condition is false. See example below.

This example sets the $x variable to 6, then it runs the loop, and then the condition is checked:

Example

$x = 6;

do {
  echo "The number is: $x
";
  $x++;
} while ($x <= 5);
?>

Try it Yourself »



(PHP 4, PHP 5, PHP 7, PHP 8)

while loops are the simplest type of loop in PHP. They behave just like their C counterparts. The basic form of a while statement is:

The meaning of a while statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to true. The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration (each time PHP runs the statements in the loop is one iteration). If the while expression evaluates to false from the very beginning, the nested statement(s) won't even be run once.

Like with the if statement, you can group multiple statements within the same while loop by surrounding a group of statements with curly braces, or by using the alternate syntax:

while (expr):
    statement
    ...
endwhile;

The following examples are identical, and both print the numbers 1 through 10:

/* example 1 */$i 1;
while (
$i <= 10) {
    echo 
$i++;  /* the printed value would be
                   $i before the increment
                   (post-increment) */
}/* example 2 */$i 1;
while (
$i <= 10):
    echo 
$i;
    
$i++;
endwhile;
?>

razvan_bc at yahoo dot com

2 months ago

assuming you want to have another way to archieve
for($i=10;$i>0;$i--){
    echo
$i.'
'
;
}
?>

then yo have this:

$v

=10;
do{
    echo
$v.'
'
;
}while(--
$v);/*
while(--$v) : 10...1 , when $v==0 stops
*/
?>

Dan Liebner

1 year ago

While loops don't require a code block (statement).

while( ++$i < 10 ); // look ma, no brackets!echo $i; // 10?>

mparsa1372 at gmail dot com

1 year ago

The example below displays the numbers from 1 to 5:

$x = 1;

while(

$x <= 5) {
  echo
"The number is: $x
"
;
 
$x++;
}
?>

This example counts to 100 by tens:

$x = 0;

while(

$x <= 100) {
  echo
"The number is: $x
"
;
 
$x+=10;
}
?>

er dot sarimkhan786 at gmail dot com

6 years ago

simple pyramid pattern program using while loop
$i=1;
while(
$i<=5)
{
   
$j=1;
    while(
$j<=$i)
    {
      echo
"*  ";
     
$j++;     
    }
    echo
"
"
;
   
$i++;
}
?>
// or alternatively you can use:
$i=1;
while(
$i<=5):$j=1;
    while(
$j<=$i):
      echo
"*  ";
     
$j++;     
    endwhile;

        echo

"
"
;
   
$i++;
endwhile;
?>

How do I run a while loop in PHP?

The PHP do-while loop is guaranteed to run at least once. The PHP do-while loop is used to execute a set of code of the program several times..
$x = 1;.
echo "1 is not greater than 10.";.
echo "
";.
} while ($x > 10);.
echo $x;.

How do you make a while loop?

Syntax. do { statement(s); } while( condition ); Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested. If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again.

Why while loop is used in PHP?

The while loop executes a block of code as long as the specified condition is true.

How is PHP looping done?

PHP supports following four loop types..
for − loops through a block of code a specified number of times..
while − loops through a block of code if and as long as a specified condition is true..
do... ... .
foreach − loops through a block of code for each element in an array..