PHP Nested Loops
PHP Nested Loops

PHP Simple Nested Loop Explained for Beginners

PHP Simple Nested Loop Explained for Beginners

Hello readers,

As we know, Nested loops are a common programming scenario where one loop is placed inside another loop. This can be done in any programming language that supports loops, such as JavaScript, PHP, Python, Java, C++, etc.

For the beginners, this maybe a little bit complicated or confusing. In this reason,  I will try to help in that area. In this short tutorial, I will explain you how PHP Simple Nested Loop works. So let’s jump right into it.

First of all: look at the below code snippets:

<?php
for ($i = 0; $i < 3; $i++) {  // Outer loop
    for ($j = 0; $j < 2; $j++) {  // Inner loop
        echo "Outer loop index: $i, Inner loop index: $j<br>";
    }
}
?>

As you can see, there is two loops, one inside another. Basic idea is, that the inner loop runs multiple times for each iterations of outer loop. What will happen here?

In this nested loop example, the outer loop runs three times (as variable $i initial value is 0, and other values of  $i can be 1 and 2. Because : $i < 3, and for each iteration of the outer loop (for $i), the inner loop runs twice (when variable $j takes the values 0 and 1, because,  variable $j < 2). The echo statement outputs the indices of both loops.

After variable $j value is not met the condition, it gets out of the loop and make value of $i = 1. Again met the condition of outside loop, goes inside second loop, executed second loop two times and it gets out of the loop by making the value if $i = 2. And again repeated one more time for this.

When this PHP script is executed, it will produce output similar to the following:

Output:

// Outer loop index: 0, Inner loop index: 0 
// Outer loop index: 0, Inner loop index: 1 
// Outer loop index: 1, Inner loop index: 0 
// Outer loop index: 1, Inner loop index: 1 
// Outer loop index: 2, Inner loop index: 0 
// Outer loop index: 2, Inner loop index: 1

Hope that will simplify your nested loop scenario.

Check Out More Resources:

Articles:
Website: https://laravelplug.com/
YouTube Channel: https://www.youtube.com/channel/UCnTxncHQcBSNs4RCuhWgF-A?sub_confirmation=1
WordPress Playlist: https://www.youtube.com/watch?v=8VvXzFAFwQU&list=PLVoVZqAh8dTLHZ51egvEU7dsOrRCSIMWx
Tools Playlist: https://www.youtube.com/watch?v=gTQoUn3BQkM&list=PLVoVZqAh8dTK-DWHBGWOEEvzvPQrgFne-

WordPress Tutorials: https://laravelplug.com/category/wordpress/
Laravel Tutorials: https://laravelplug.com/category/laravel/
PHP Tutorials: https://laravelplug.com/category/php/
SQL Tutorials: https://laravelplug.com/category/sql/
Various Tips: https://laravelplug.com/category/tips/
Useful Tools: https://laravelplug.com/category/tools/

Socials:

Twitter: https://twitter.com/LaravelPlug
Pinterest: https://www.pinterest.com/LaravelPlugCom/
Facebook: https://www.facebook.com/groups/1020759861842360
Mail: info@laravelplug.com

#WordPress #guide #search

Editorial Staff

A Learner and trying to share what I learned!