Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
PHP While Loop Flowchart
3.
Understanding PHP While Loop
3.1.
What is a While Loop?
3.2.
The Syntax of While Loop
4.
Using PHP While Loops
4.1.
Example 1: Basic Usage
4.2.
PHP
4.3.
Example 2: While Loop with Arrays
4.4.
PHP
4.5.
Example 3: The Do-While Loop
4.6.
PHP
5.
Nested While Loop in Php
5.1.
PHP
6.
Example that Uses While and Endwhile to Display the Numbers
6.1.
PHP
6.2.
Explanation
7.
Frequently Asked Questions
7.1.
What is do-while loop in PHP?
7.2.
What is the explanation of while loops?
7.3.
What is a loop in PHP?
8.
Conclusion
Last Updated: Jul 17, 2024
Easy

While Loop in PHP

Author Gunjan Batra
0 upvote

Introduction

The power of programming languages is their ability to perform complex tasks quickly and accurately repetitively. In PHP, a server-side scripting language, one of the cornerstones of this repeated action is the 'While' loop. 

While loop in PHP

In this article, we'll go through the basics of the While loop and show you some practical examples.

PHP While Loop Flowchart

Creating a textual flowchart in this format is challenging due to the limitations of text representation. However, I can provide a simplified description of the flowchart for a PHP while loop:

  1. Start: Begin at the starting point.
  2. Initialization: Initialize the loop control variable (e.g., $i = 0) before the loop.
  3. Condition Check: Check the condition before entering the loop (while ($i < 10)).
  4. True Path: If the condition is true, enter the loop. Execute the statements inside the loop.
  5. Update Variable: Update the loop control variable inside the loop (e.g., $i++).
  6. Back to Condition Check: Go back to the condition check. If the condition is still true, repeat the loop. If false, exit the loop.
  7. False Path: If the condition is false from the start or becomes false during execution, exit the loop.
  8. End: End the flowchart.
PHP While Loop Flowchart

Understanding PHP While Loop

What is a While Loop?

A 'While' loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.

The Syntax of While Loop

The basic syntax of a while loop in PHP is as follows:

while (condition) {
   // Code to be executed
}

The loop continues until the condition evaluates to false.

Using PHP While Loops

Now, let's explore the power of while loops with some examples.

Example 1: Basic Usage

Here's a simple while loop that will print numbers 1 to 5:

  • PHP

PHP

$counter = 1;
while ($counter <= 5) {
   echo $counter . "<br>";
   $counter++;
}
You can also try this code with Online PHP Compiler
Run Code


Output

output

Example 2: While Loop with Arrays

A while loop can also work with arrays. Let's go through an array of names:

  • PHP

PHP

$names = array("John", "Jane", "Joe");
$index = 0;
while($index < count($names)) {
   echo $names[$index] . "<br>";
   $index++;
}
You can also try this code with Online PHP Compiler
Run Code


Output

output

Example 3: The Do-While Loop

The do-while loop is a variant that executes the code block once before checking the condition. It's useful when the block needs to run at least once:

  • PHP

PHP

$counter = 1;
do {
   echo $counter . "<br>";
   $counter++;
} while ($counter <= 5);
You can also try this code with Online PHP Compiler
Run Code


Output

output

Nested While Loop in Php

 In PHP, nested while loops involve placing one while loop inside another. This is often used when dealing with two-dimensional data structures or situations where you need to iterate over multiple levels. Here's an example of a nested while loop in PHP:

  • PHP

PHP

<?php
// Outer loop
$row = 1;
while ($row <= 3) {
echo "Row $row: ";
// Inner loop
$col = 1;
while ($col <= 4) {
echo "$col ";
$col++;
}
echo "<br>";
// Increment the outer loop counter
$row++;
}
?>
You can also try this code with Online PHP Compiler
Run Code

 

This example prints numbers in a table-like format:

Row 1: 1 2 3 4 
Row 2: 1 2 3 4 
Row 3: 1 2 3 4 

Example that Uses While and Endwhile to Display the Numbers

In PHP, the while loop executes a block of code as long as the specified condition evaluates to true. The endwhile statement can also be used to end the while loop. Here's an example:

  • PHP

PHP

<?php
$number = 1;
while ($number <= 5):
echo "The number is: $number <br>";
$number++;
endwhile;
?>
You can also try this code with Online PHP Compiler
Run Code

Explanation

  1. Initialization: The variable $number is initialized to 1.
     
  2. Condition: The while loop checks if $number is less than or equal to 5.
     
  3. Execution: If the condition is true, the code inside the loop is executed:
    • It prints the current value of $number.
       
    • It increments $number by 1.
       
  4. Loop End: The endwhile statement marks the end of the while loop.
     
  5. Repeat: The process repeats until $number is greater than 5.


Output:

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

 

In this example, the while loop displays the numbers from 1 to 5, demonstrating the use of while and endwhile in PHP.

Frequently Asked Questions

What is do-while loop in PHP?

A do-while loop in PHP executes a block of code at least once, and then repeats the execution as long as the specified condition is true.

What is the explanation of while loops?

A while loop in PHP repeatedly executes a block of code as long as the specified condition evaluates to true.

What is a loop in PHP?

A loop in PHP is a control structure that allows repeated execution of a block of code until a specified condition is met.

Conclusion

In conclusion, PHP's While loop is a powerful tool for any developer. It allows for the repeated execution of code based on a specified condition, increasing efficiency and readability. Understanding the syntax, usage, and potential pitfalls of While loops can greatly improve your ability to write efficient, effective PHP code. Practice with different scenarios to fully understand how this versatile loop works. Keep coding, and the concepts will become second nature!

If you would like to learn more, check out our articles on

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Learning Ninja!

Live masterclass