WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 while Loops


PHP while circles will execute a square of code if and up to a test articulation is true.


PHP Loops

When composing code, you may need a similar square of code to keep running again and again. including a few practically approach code-lines in a content, we can utilize circles to play out an undertaking like this.

In PHP, we have the accompanying circling statements:

  • while -The while circle executes a square of code as long as the predetermined condition is true.
  • do...while - circles through a square of code once, and after that rehashes the circle for whatever length of time that the predetermined condition is true
  • for -circles through a square of code a predefined number of times
  • foreach -The foreach circle works just on clusters, and is utilized to circle through each key/esteem pair in an exhibit.

The PHP while Loop

The while circle executes a square of code as long as the predetermined condition is true.

Syntax

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

The precedent beneath first sets a variable $x to 2 ($x = 2). At that point, the while circle will keep on running as long as $x is not exactly, or equivalent to 5 ($x <= 6). $x will increment by 2each time the circle runs ($x++):

Example

<?php
$x = 2;

while($x <=6) {
    reverberation "The number is: $x <br>";
    $x++;
}
?>
 

yield// 

The number is: 2 

The number is: 3 

The number is: 4 

The number is: 5 

The number is: 6 


The PHP do...while Loop

The do...while circle will dependably execute the square of code once, check the condition, and rehash the circle as long as the predefined condition is valid.

Syntax

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

The model underneath first sets a variable $x to 2 ($x = 2). At that point, the do while circle will think of some yield, and after that increase the variable $x with 2. At that point the condition is checked (is $x not exactly, or equivalent to 5?), and the circle will keep on running as long as $x is not exactly, or equivalent to 6:

Example

<?php
$x = 1;

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

The number is: 2 

The number is: 3 

The number is: 4 

The number is: 5 

The number is: 6 

Notice that in a do while circle the condition is tried AFTER executing the announcements inside the circle. This implies the do while circle would execute its announcements in any event once, regardless of whether the condition is false the first time.

The model beneath sets the $x variable to 6, at that point it runs the circle, and afterward the condition is checked:

Example

$i = 6; do { reverberation "The number is " . $i . "
"; $i++; } while($i <= 7); /Output /The number is 6 /The number is 7