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

Related Article

Blog

PHP Magic Methods

__get()/__set() – These are magic getters and setters for getting and putting values for class properties created dynamically. __isset() – This magic method will be invoked automatically while checking whether a required overloaded property is Read more…

Blog

WordPress Upgrade

The WordPress upgrade released time to time. We provide a WordPress upgrade, content management and maintenance services at affordable price. WordPress upgrade fixes bugs, runs cleaner, faster, and is more secure and stable. We will Read more…

Blog

Magento 2 Upload image in product

If you are trying to upload image on respective product by script then below code will help you to solve this issue. by this code you can set product thumbnail, small and large image. $objectManager Read more…