Loops in PHP - for, while, foreach, do-while
Categories - PHP Tags - Programming PHP   Maniruzzaman Akash   4 months ago   257   0

Loops in PHP - for, while, foreach, do-while

What is Loop in Programming?

In programming to run a repeatable blocks of code until a specific condition is met, we use Loop. Loop is not for only PHP programming things, it’s same in almost all of the programming languages.

So let’s break down it some parts -

  1. Run a repeatable blocks of code
  2. Code Stop executing when
  3. A Stop condition is met.

Real-life Example of Loop in Daily Life

  1. Morning Walk - We do Morning walk until our specific amount of walking is completed.
  2. Tooth Brush - We start brush our tooth until we finish brushing all of the tooths. When every tooth is finished brushing, we stop brush our tooth.

Real-life Example of Loop in Programming Language

  1. Even Numbers: We need to show all even numbers upto 100. Here 0 = Start, <= 100 = Stop condition, printing number is the statement
  2. Multiplication-table: We need to make a multiplication table of any number. Example: Multiple table of 1. We start from 1, Stop when cycle at 10 times. Every times, we increment number.

Type of Loops in PHP

There are 4 types of loops in PHP. They are -

  1. for loop
  2. while loop
  3. do-while loop
  4. foreach loop.

for loop in PHP

for loop is one type of loop in PHP or any other programming language. In PHP for loops syntax is like C language.

Structure of for loop

for (initalStatement; testCondition; IncrementOrDerementStatement)
{
    // statements inside the body of loop
}

Parts of a for loop:

  1. initial Statement: In First part, we initialize something and start the cycle
  2. Stop Condition: In Second part, we give a boolean like expression or condition. If the condition is true, the cycle will go to the Body statement and continue.
  3. Increment or Decrement Statement: In Third part, we actually give another expression, which can increment or decrement the checking value previously used. So that after every cycle this increase or decrease fired and after a certain amount of time, we meet the Stop condition.

Example of a for loop: Print Numbers 1 to 10

<?php

for($i = 0; $i <= 10; $i++) {
	echo $i . '<br>';
}

?>
0
1
2
3
4
5
6
7
8
9
10

Explanations:

Look this line - for($i = 0; $i <= 10; $i++) {

  1. $i = 0 is the initialization for this loop. That means - when something enter into the body statement of the loop, $i will be 0.
  2. $i <= 10 is the Stop condition. Unless $i’s value is less than or equal to 10, the condition is true and it will continue to execute the body statement.
  3. $i++ is the incremental operator, we’ve discussed previously. This will fire after the execution of the body. That means $i will be than 1 after first cycle.

Overview of every iteration/cycle

// First Cycle: $i = 0;  0 <= 10 is True >> Print the $i = 0
// Second Cycle: $i = 1;  1 <= 10 is True >> Print the $i = 1
// Third Cycle: $i = 2;  2 <= 10 is True >> Print the $i = 2
// Fourth Cycle: $i = 3;  3 <= 10 is True >> Print the $i = 3
// Fifth Cycle: $i = 4;  4 <= 10 is True >> Print the $i = 4
// Sixth Cycle: $i = 5;  5 <= 10 is True >> Print the $i = 5
// Seventh Cycle: $i = 6;  6 <= 10 is True >> Print the $i = 6
// Eighth Cycle: $i = 7;  7 <= 10 is True >> Print the $i = 7
// Ninth Cycle: $i = 8;  8 <= 10 is True >> Print the $i = 8
// Ten Cycle: $i = 9;  9 <= 10 is True >> Print the $i = 9
// Eleventh Cycle: $i = 10;  10 <= 10 is True >> Print the $i = 10
// Twelveth Cycle: $i = 11;  111 <= 10 is False >> Don't Print the loop, Stops executing the loop

That’s awesome, it’s the simple logics of how a basic loop actually works. And all other loops also follow the same pattern. Learn more at PHPNet - https://www.php.net/manual/en/control-structures.for.php

while loop in PHP

while loop is kind of loop in PHP which executes body statement, until the while’s parameter condition or expression is true. In PHP while loops syntax is like C language.

Structure of while loop

while(conditionOrExpression) {
 // Execute the body statement
}

Example of a while loop: Print Numbers 1 to 10

<?php

$i = 0;
while($i <= 10) {
	echo $i . '<br>';
	$i++;
}

?>
0
1
2
3
4
5
6
7
8
9
10

Explanations:

First Iteration:

Look this line - $i = 0;

  1. $i = 0 is the initialization for this loop. That means - when the loop starts $i will be 0.
  2. It will check the while condition $i <= 10, means 0 <= 10. Yes, the condition is true, then the statement inside while loop will execute and echo the $i.
  3. Then in $i++;, we assign the $i value = $i + 1 actually, $i = 1,

Second Iteration:

  1. Now, from previous iteration $i = 1.
  2. It will check the while condition $i <= 10, means 1 <= 10. Yes, the condition is true, then the statement inside while loop will execute and echo the $i.
  3. Then in $i++;, we assign the $i value = $i + 1 actually, $i = 2,

… Rest of the Iteration will be the same until 11th iteration.

Eleventh Iteration:

  1. Now, from previous iteration $i = 11.
  2. It will check the while condition $i <= 10, means 11 <= 10. No, the condition is false, then the statement inside while loop will not execute and don’t process the next steps.

Learn more in PHPNet - https://www.php.net/manual/en/control-structures.while.php

do-while loop in PHP

do-while loop is almost same like as while loop and one difference is that the truth expression is checked at the end of each cycle/iteration instead of in the beginning.

Structure of do-while loop

// initialStatement
do {
   // Execute the body statement
} while(conditionOrExpression);

Example of a do-while loop: Print Numbers 1 to 10

<?php

$i = 0;
do {
	echo $i . '<br>';
	$i++;
} while($i <= 10);

?>
0
1
2
3
4
5
6
7
8
9
10

Learn more at PHPNet - https://www.php.net/manual/en/control-structures.do.while.php

foreach loop in PHP

foreach loop is a special type loop that only works for iteration of array and object.

Structure of foreach loop

foreach (iterable_expression as $value) {
    // Execute statement
}

Example of a foreach loop: Print Numbers 1 to 10

<?php

$numbers = [ 1, 2, 3, 4, 5, 6, 8, 9, 10 ];

foreach($numbers as $number) {
	echo $number . '<br>';
}
?>
0
1
2
3
4
5
6
7
8
9
10

Learn more at PHPNet - https://www.php.net/manual/en/control-structures.foreach.php

More Examples of Loops

Example - Print Even Numbers Upto 1 to 100

<?php

// For Loop Example:
for($i = 0; $i <= 100; $i += 2) {
	echo $i . '<br>';
}

// While loop example
$i = 1;

while($i <= 100) {
	echo $i . '<br>';
	$i += 2;
}

?>

Example - Print Odd Numbers Upto 1 to 100

<?php

for($i = 1; $i <= 100; $i += 2) {
	echo $i . '<br>';
}

?>

Example - Multiplication Number of any number.

<?php
$number = 5;

for($i = 1; $i <= 10; $i++) {
	$multiplication = $i * $number;
	echo $number . ' X ' . $i . ' = ' .  $multiplication . '<br>';
}

?>

Output:

5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
Previous
PHP String Functions - All necessary String functions in PHP to manage strings better.
Next
PHP Array and all it's Examples