Blog Introductions
In this blog contains tutorials about HTML Tutorials, Python Tutorials, CSS Tutorials, How to, PHP Tutorials, Java Tutorials, C++ Tutorials, Tutorials, Examples, Source code,Learning,
Tips and Software development services. Php Control flow Loops statements | Php tutorials Bestitworriors
Php Control flow Loops statements
Php loops use to iterate the pice of code for specific time .The main purpose of loops is to run the same code multiple time on a specific time interval.Php have many type of loop to perform the tesk using loops .Php loop have while loop , do-while loop ,for loop and for-each loop to perform the loop task .The purpose is same but the code structure and condations varry in all loops like for-each loop mostely use iterate the arrays
Some Key Points of PHP Loops
- Php loops iterate specific code multiple time
- While loop iterate untill specific condation is true
- Do-while loop iterate first and the check condation and the iterate untill the specific condation
- For-loop iterate on a specified condation
- For-each loop iterate untill each item in an array
- All type of loops have same work but little code structure is different
- Loops purpose is same but work on different problems
- The break statement use jump out of loop
- Continue statement use to skip the specific iteration
Code
<?php
echo "While Loop<br>";
$condation = 1;
while($condation <= 5) {
echo "The Condation is: $condation <br>";
$condation++;
}
echo "<br>";
echo "Do While Loop <br>";
$condation1 = 1;
do {
echo "The Condation is: $condation1 <br>";
$condation1++;
} while ($condation1 <= 5);
echo "<br>";
echo "For Loop<br>";
for ($cond = 0; $cond <= 5; $cond++) {
echo "The Condation is: $cond <br>";
}
echo "<br>";
echo "For each Loop<br>";
$months = array("january", "feburary", "march", "april");
foreach ($months as $value) {
echo "$value <br>";
}
echo "<br>";
echo "Break Statement";
echo "<br>";
//The break statement can also be used to jump out of a loop.
for ($cond2 = 0; $cond2 < 5; $cond2++) {
if ($cond2 == 4) {
break;
}
echo "The Condation is: $cond2 <br>";
}
echo "<br>";
echo "Continue Statement";
echo "<br>";
//The continue statement breaks one iteration
for ($con = 0; $con <5; $con++) {
if ($con == 4) {
continue;
}
echo "The number is: $con <br>";
}
?>
OutPut
While Loop
The Condation is: 1
The Condation is: 2
The Condation is: 3
The Condation is: 4
The Condation is: 5
Do While Loop
The Condation is: 1
The Condation is: 2
The Condation is: 3
The Condation is: 4
The Condation is: 5
For Loop
The Condation is: 0
The Condation is: 1
The Condation is: 2
The Condation is: 3
The Condation is: 4
The Condation is: 5
For each Loop
january
feburary
march
april
Break Statement
The Condation is: 0
The Condation is: 1
The Condation is: 2
The Condation is: 3
Continue Statement
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The Condation is: 1
The Condation is: 2
The Condation is: 3
The Condation is: 4
The Condation is: 5
Do While Loop
The Condation is: 1
The Condation is: 2
The Condation is: 3
The Condation is: 4
The Condation is: 5
For Loop
The Condation is: 0
The Condation is: 1
The Condation is: 2
The Condation is: 3
The Condation is: 4
The Condation is: 5
For each Loop
january
feburary
march
april
Break Statement
The Condation is: 0
The Condation is: 1
The Condation is: 2
The Condation is: 3
Continue Statement
The number is: 0
The number is: 1
The number is: 2
The number is: 3
0 Comments