PHP Loop: For, ForEach, While, Do While [Example – for loop in php]

Today, We want to share with you for loop in php.In this post we will show you php for loop array length, hear for PHP Loop: For, ForEach, While, Do While [Example] we will give you demo and example for implement.In this post, we will learn about PHP Nested While Loops And Multiple Conditions with an example.

PHP While, Do-While, For and Foreach Loops

PHP for Loop

There are the following the Different Types of Loops in PHP

  • while
  • do…while
  • for
  • foreach

Example 1:

<!DOCTYPE html>
<html>
<body>

<?php  
for ($counter = 0; $counter <= 10; $counter++) {
  echo "The Counter is: $counter <br>";
}
?>  

</body>
</html>

Results:

The Counter is: 0
The Counter is: 1
The Counter is: 2
The Counter is: 3
The Counter is: 4
The Counter is: 5
The Counter is: 6
The Counter is: 7
The Counter is: 8
The Counter is: 9
The Counter is: 10

Example 2:

<!DOCTYPE html>
<html>
<body>

<?php  
for ($counter = 0; $counter <= 100; $counter+=10) {
  echo "The counter is: $counter <br>";
}
?>  

</body>
</html>


Results:
The counter is: 0
The counter is: 10
The counter is: 20
The counter is: 30
The counter is: 40
The counter is: 50
The counter is: 60
The counter is: 70
The counter is: 80
The counter is: 90
The counter is: 100

PHP while Loop

<?php
$counter = 1;
while($counter <= 3){
    $counter++;
    echo "The counter is " . $counter . "<br>";
}
?>

PHP do…while Loop

<?php
$counter = 1;
do{
    $counter++;
    echo "The counter is " . $counter . "<br>";
}
while($counter <= 3);
?>

PHP foreach Loop

<?php
$websiteLists = array("pakainfo", "infinityknow", "4cgandhi");
 
// Loop through websiteLists array
foreach($websiteLists as $value){
    echo $value . "<br>";
}
?>

<?php
$websiteInfo = array(
    "name" => "pakainfo Welcome",
    "email" => "[email protected]",
    "age" => 25
);
 
// Loop through websiteInfo array
foreach($websiteInfo as $key => $value){
    echo $key . " : " . $value . "<br>";
}
?>

I hope you get an idea about for loop in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.