PHP While Loop

PHP while loop executes a set of statements as long as a condition is true.

It requires relevant variables to be ready such as an indexing variable to iterate through a loop.


A while loop checks if the specified condition is true after each iteration. .


Glossary:

LoopIn computer programming, loops are instructions that repeat until a specified condition is reached.
IterationEach run or execution of the loop
<?php
$counter = 0;
while ($counter < 10)
{
	$counter += 1; // Increment By One
	echo "Executing - counter is $counter.\n"; // Print Outpur
}
?>

Explanation:

  1. Loop starts when the variable $counter is set to zero.
  2. For the first iteration of the loop, the variable $counter is incremented by one.
  3. For the second iteration of the loop, the variable $counter is incremented by one to become two.
  4. For the third iteration of the loop, the variable $counter is incremented by one to become three.
  5. For the fourth iteration of the loop, the variable $counter is incremented by one to become four.
  6. For the fifth iteration of the loop, the variable $counter is incremented by one to become five.
  7. For the sixth iteration of the loop, the variable $counter is incremented by one to become six.
  8. For the seventh iteration of the loop, the variable $counter is incremented by one to become seven.
  9. For the eighth iteration of the loop, the variable $counter is incremented by one to become eight.
  10. For the ninth iteration of the loop, the variable $counter is incremented by one to become nine.
  11. The tenth and final iteration of the loop, the variable $counter is incremented by one to become ten which is no less than the condition statement ($counter < 10).

This example above can be found in Learning PHP which is an online ebook. The only requirement is a web browser. You can use the built-in Web IDE to view your code and compile it online to view the result. You pay once for the online book and get access to the book at anytime without extra costs.


OjamboShop.com Learning PHP While Loop Example
OjamboShop.com Learning PHP While Loop Example

Limited Time Offer:

OjamboShop.com is offering 50% off coupon code VICTORIA for Learning PHP until Victoria Day 2024.


Conclusion:

Take this opportunity to learn PHP programming language by making a one-time purchase at Learning PHP. A web browser is the only thing needed to learn PHP in 2024 at your leisure. All the developer tools are provided right in your web browser.


References: