Introduction
One of the simplest and most flexible loops in PHP is the while loop. It is especially useful when the number of iterations is not known in advance and depends on a condition.
The while loop checks a condition before executing the code block, making it a pre-test loop.
What is a PHP While Loop?
The while loop in PHP is used to execute a block of code repeatedly as long as a specified condition is true.
In simple terms:
- If the condition is true β code executes
- If the condition is false β loop stops
- Condition is checked before execution
- Executes zero or more times
- Used when the number of iterations is unknown
- Can create infinite loops if not handled properly
Syntax
Basic Syntax:
while (condition) {
// code to execute
}
Explanation:
- condition β evaluated before each iteration
- If true β loop continues
- If false β loop stops
Examples:
Example 1: Basic While Loop
<?php
$i = 1;
while($i < 5) {
print $i . "<br/>";
$i++;
}
?>
Output:
2
3
4
Example 2: Decrement Loop
<?php
$i = 1;
while($i < 5) {
echo $i;
}
?>
Example 3: Infinite Loop (Avoid This)
<?php
$i = 1;
while($i < 5) {
echo $i;
}
?>
This will run forever because $i is never incremented.
Example 4: Using Break Statement
<?php
$i = 1;
while($i <= 10) {
if($i == 5) {
break;
}
echo $i . "<br/>";
$i++;
}
?>
Example 5: Using Continue Statement
<?php
$i = 0;
while($i < 5) {
$i++;
if($i == 3) {
continue;
}
echo $i . "<br/>";
}
?>
Real-Life Example
Scenario: Reading Data Until Condition is Met
<?php
$password = "";
while(empty($password)) {
$password = "user123"; // suppose user input
}
echo "Password accepted";
?>
Why this is useful:
- Waits until valid input is received
- Useful in login systems
- Helps in validation loops
Scenario 2: Processing Array Data
<?php
$items = ["Laptop", "Mobile", "Tablet"];
$i = 0;
while($i < count($items)) {
echo $items[$i] . "<br/>";
$i++;
}
?>
Note: Used in real applications like e-commerce product listing.
Common Mistakes
1. Infinite Loop
while($i < 5) {
echo $i;
}
Note: Missing increment causes an infinite loop.
2. Wrong Condition
while($i > 5)
Note: The loop may never execute.
3. Forgetting Initialization
while($i < 5)
Note: $i is not defined.
4. Updating Variable Incorrectly
Wrong increment/decrement logic can break loop flow.
5. Using While Instead of For
If the number of iterations is known, a for loop is better.
Conclusion
The PHP while loop is a powerful and flexible looping structure that allows you to execute code based on conditions. It is especially useful when you donβt know how many times the loop should run.
By understanding the while loop, you can:
- Handle dynamic conditions
- Process data efficiently
- Build interactive applications
PHP While loop β Interview Questions
Q 1: What is while loop?
Q 2: When is the condition checked?
Q 3: Can while loop run zero times?
Q 4: Syntax example?
Q 5: Risk of while loop?
PHP While loop β Objective Questions (MCQs)
Q1. The while loop checks condition ______ executing the loop.
Q2. If the condition is false initially, while executes ______.
Q3. Which keyword skips the current iteration?
Q4. Missing condition update may cause ______.
Q5. while loop is ideal when iterations are ______.