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:
Loop
In computer programming, loops are instructions that repeat until a specified condition is reached.Iteration
Each 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:
- Loop starts when the variable $counter is set to zero.
- For the first iteration of the loop, the variable $counter is incremented by one.
- For the second iteration of the loop, the variable $counter is incremented by one to become two.
- For the third iteration of the loop, the variable $counter is incremented by one to become three.
- For the fourth iteration of the loop, the variable $counter is incremented by one to become four.
- For the fifth iteration of the loop, the variable $counter is incremented by one to become five.
- For the sixth iteration of the loop, the variable $counter is incremented by one to become six.
- For the seventh iteration of the loop, the variable $counter is incremented by one to become seven.
- For the eighth iteration of the loop, the variable $counter is incremented by one to become eight.
- For the ninth iteration of the loop, the variable $counter is incremented by one to become nine.
- 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.
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.