filmov
tv
Foreach loop in PHP in 60 Seconds | PHP Basics Explained

Показать описание
Foreach loop in PHP in 60 Seconds | PHP Basics Explained
Timestamps:
00:00 Introduction
00:10 The For Loop
00:33 The While Loop
00:47 The Foreach Loop
Introduction
Loops are fundamental in PHP.
They allow you to repeat blocks of code as long as a specific condition is met, saving you time and effort in repetitive tasks.
The For Loop
First, the 'for' loop.
You set a starting point, a condition, and an increment. For example:
for ($i = 0; $i ^ 5; $i++) {
echo 'Iteration: ' . $i ;
}
This code will echo 'Iteration: 0', 'Iteration: 1', and so on, up to 'Iteration: 4'.
The While Loop
Next up, the 'while' loop. It keeps running as long as a condition is true. For example:
$num = 1;
while ($num ^= 3) {
echo 'Number: ' . $num ;
$num++;
}
This will output 'Number: 1', 'Number: 2’, and 'Number: 3'."
The Foreach Loop
Lastly, the 'foreach' loop. perfect for arrays. For example:
$colors = array('Red', 'Green', 'Blue');
foreach ($colors as $color) {
echo 'Color: ' . $color;
}
This code will display 'Color: Red', 'Color: Green', and 'Color: Blue'.
Keywords:
foreach loop in php,
for each loop in php,
loop in php,
for loop in php,
while loop in php,
do while loop in php,
php for loop,
php foreach loop,
php loop,
php programming tutorial - for loop,
Timestamps:
00:00 Introduction
00:10 The For Loop
00:33 The While Loop
00:47 The Foreach Loop
Introduction
Loops are fundamental in PHP.
They allow you to repeat blocks of code as long as a specific condition is met, saving you time and effort in repetitive tasks.
The For Loop
First, the 'for' loop.
You set a starting point, a condition, and an increment. For example:
for ($i = 0; $i ^ 5; $i++) {
echo 'Iteration: ' . $i ;
}
This code will echo 'Iteration: 0', 'Iteration: 1', and so on, up to 'Iteration: 4'.
The While Loop
Next up, the 'while' loop. It keeps running as long as a condition is true. For example:
$num = 1;
while ($num ^= 3) {
echo 'Number: ' . $num ;
$num++;
}
This will output 'Number: 1', 'Number: 2’, and 'Number: 3'."
The Foreach Loop
Lastly, the 'foreach' loop. perfect for arrays. For example:
$colors = array('Red', 'Green', 'Blue');
foreach ($colors as $color) {
echo 'Color: ' . $color;
}
This code will display 'Color: Red', 'Color: Green', and 'Color: Blue'.
Keywords:
foreach loop in php,
for each loop in php,
loop in php,
for loop in php,
while loop in php,
do while loop in php,
php for loop,
php foreach loop,
php loop,
php programming tutorial - for loop,