PHP While Loop

Revised 2 min, 21 sec read

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:

🚀 Recommended Resources


Disclosure: Some of the links above are referral links. I may earn a commission if you make a purchase at no extra cost to you.

About Edward

Edward is a software engineer, author, and designer dedicated to providing the actionable blueprints and real-world tools needed to navigate a shifting economic landscape.

With a provocative focus on the evolution of technology—boldly declaring that “programming is dead”—Edward’s latest work, The Recession Business Blueprint, serves as a strategic guide for modern entrepreneurship. His bibliography also includes Mastering Blender Python API and The Algorithmic Serpent.

Beyond the page, Edward produces open-source tool review videos and provides practical resources for the “build it yourself” movement.

📚 Explore His Books – Visit the Book Shop to grab your copies today.

💼 Need Support? – Learn more about Services and the ways to benefit from his expertise.

🔨 Build it Yourself – Download Free Plans for Backyard Structures, Small Living, and Woodworking.