In this article while Loop in PHP we give the information about the while loop is used when we want to ensure that the code runs repeatedly, and a condition is given for the termination of the loop.
while Loop in PHP:
Syntax of while Loop
while (condition) {
//This code will run as long as the condition is true.
}
Description of parts of syntax
- Condition:
The loop will continue as long as the condition is true.
- Body of the Loop:
It contains code that will be run repeatedly.
While Loop Process (Execution Process)
- First of all, the condition is checked.
- If the condition is true, the code in the loop will be run.
- After the code runs, the condition will be checked again.
- This process is repeated until the condition becomes false.
Example 1: Printing numbers from 1 to 5
<?php
$i = 1; //initial value
while ($i<= 5) {
echo “Number: $i<br>”;
$i++; //increment the variable by 1
}
?>
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Example 2: Printing Reverse Numbers from 10 to 1
<?php
$i = 10;
while ($i>= 1) {
echo “Number: $i<br>”;
$i–; //decrement the variable by 1
}
?>
Output:
Number: 10
Number: 9
Number: 8
Number: 7
Number: 6
Number: 5
Number: 4
Number: 3
Number: 2
Number: 1
Example 3: Printing only even numbers
<?php
$i = 2;
while ($i<= 10) {
echo “Even number: $i<br>”;
$i += 2; //increment the variable by 2
}
?>
Output:
Even number: 2
Even number: 4
Even number: 6
Even number: 8
Even number: 10
Infinite Loop
If the condition never returns false, the loop will continue indefinitely.
Example
<?php
$i = 1;
while ($i<= 5) {
echo “Number: $i<br>”;
// Forgot to increment $i, which will cause the loop to become infinite.
}
?>
Stopping the loop (break statement)
break is used to end the loop midway.
Example
<?php
$i = 1;
while ($i<= 10) {
if ($i == 5) {
break; //The loop will stop at 5.
}
echo “Number: $i<br>”;
$i++;
}
?>
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Skipping iteration (continue statement)
Continue is used to skip the current iteration and move to the next.
Example
<?php
$i = 1;
while ($i<= 5) {
if ($i == 3) {
$i++; //Increment $i before moving on to the next iteration.
continue; // skip 3
}
echo “Number: $i<br>”;
$i++;
}
?>
Output:
Number: 1
Number: 2
Number: 4
Number: 5
Tips for using while Loop
- Avoid Infinite Loop:
Make sure the loop condition is defined correctly and the variables are updated correctly.
- Pay attention to the condition:
Constantly keep an eye on the condition of the loop so that it ends at the right time.
- Use break and continue in complex loops:
Use these to control code in long loops.
Conclusion
- Use while loop when the number of iterations of the loop is not known in advance.
- This continues as long as the condition remains true.
- Use this carefully so as not to create an infinite loop.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining