PHP
Loops in PHP - for, while, foreach, do-while
- What is Loop in Programming?
- Type of Loops in PHP
- More Examples of Loops
¶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 -
- Run a repeatable blocks of code
- Code Stop executing when
- A Stop condition is met.
¶Real-life Example of Loop in Daily Life
- Morning Walk - We do Morning walk until our specific amount of walking is completed.
- 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
- Even Numbers: We need to show all even numbers upto 100. Here 0 = Start, <= 100 = Stop condition, printing number is the statement
- 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 -
- for loop
- while loop
- do-while loop
- 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:
- initial Statement: In First part, we initialize something and start the cycle
- 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.
- 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>';
}
?>
1
2
3
4
5
6
7
8
9
10
¶Explanations:
Look this line - for($i = 0; $i <= 10; $i++) {
-
$i = 0
is the initialization for this loop. That means - when something enter into the body statement of the loop,$i
will be0
. -
$i <= 10
is the Stop condition. Unless$i
’s value is less than or equal to10
, the condition is true and it will continue to execute the body statement. -
$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++;
}
?>
1
2
3
4
5
6
7
8
9
10
¶Explanations:
¶First Iteration:
Look this line - $i = 0;
-
$i = 0
is the initialization for this loop. That means - when the loop starts$i
will be0
. - It will check the while condition
$i <= 10
, means0 <= 10
. Yes, the condition is true, then the statement inside while loop will execute and echo the$i
. - Then in
$i++;
, we assign the $i value = $i + 1 actually, $i = 1,
¶Second Iteration:
- Now, from previous iteration
$i = 1
. - It will check the while condition
$i <= 10
, means1 <= 10
. Yes, the condition is true, then the statement inside while loop will execute and echo the$i
. - 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:
- Now, from previous iteration
$i = 11
. - It will check the while condition
$i <= 10
, means11 <= 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);
?>
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>';
}
?>
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 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
PHP String Functions - All necessary String functions in PHP to manage strings better.
PHP Array and all it's Examples
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development