The while condition will carry out a block of code if and as long as an expression is true. If the expression is true then the code block will be executed until the expression is found to be false.
Syntax:-
while (condition)

{

Execution code..;

}
Example:-
<?php

$J = 0;

$num = 40;

While( $J < 10) {

$num–;

$J++;

}

echo (“Loop stopped at J = $J and num = $num” );

?>

This will produce the following result −

Loop stopped at J = 10 and num = 30
do…while Loop:-
The do…while condition will execute a block of code at least once and repeat the loop as long as a condition is true.
Syntax:-
do {

Execution code..;

}while (condition);
Example:-

<?php

$J = 0;

$num = 0;

do {

$J++;

}  while( $J < 10 );

echo (“Loop stopped at J = $J” );

?>

This will produce the following result:−

Loop stopped at J = 10;

Categories: PHP

0 thoughts on “PHP While Loop”

Leave a Reply