Nested loops in PHP explained

Опубликовано: 19 Март 2025
на канале: PHP Explained
100
2

Nested loops means one loop contains another loop.

There are four ideal loop structure available in PHP and these are while, do while, for, and foreach loop.

Any of these four types of loops can contain each other to form nested loop. There is no limitation of nested number of loops.

Let's explain with an example.

Here we are using for loop.
First for loop called outer for loop contains another for loop called inner for loop.

Outer loop consists of variable i and iterates for 10 times.

Inner loop consists of variable j and iterates for 10 times also.

For one iteration of the outer loop, the inner loop iterates 10 times. So, all together 10 times outer multiplied by 10 times inner iterations, equal to 100, hence the loops are iterating for 100 times.

for ($i = 1; $i lt;= 10; $i++)
{
for ($j = 1; $j lt;= 10; $j++)
{
echo $i . " " . $j . "\n";
}
}


На этой странице сайта вы можете посмотреть видео онлайн Nested loops in PHP explained длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь PHP Explained 19 Март 2025, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 100 раз и оно понравилось 2 зрителям. Приятного просмотра!