Today in this tutorial we are going to learn how to use different kinds of loops in PHP with example. We are going to see how different kinds of data works in different scenarios in different type of loops.
We are going to use different kinds of data like array of strings, object data and array of objects and see how can we do array iteration in PHP and display the data in the your page.
There are different kinds of techniques to loop through array PHP. They are mentioned below :-
- foreach loop (maximum used).
- for loop.
- while loop.
- doWhile loop.
We are going to use these loops below in our different kinds of data we will prepare below. Let’s prepare the data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// normal array of string $array_string = ['Elias P Bone', 'Mary J Washington', 'Kevin S Wheeler', 'Donald J Mc Daniel', 'Paula P Cruz']; // data with key value pairs $object_data = [ 'name' => 'Elias P Bone', 'address' => '3889 Hurry Street', 'phone' => '010012000010101', 'email' => 'someonesemail@email.com' ]; // array data of key value pairs. $array_object_data = [ [ 'name' => 'Elias P Bone', 'address' => '3889 Hurry Street' ], [ 'name' => 'William W. Lanning', 'address' => 'Naples, FL 33940' ], [ 'name' => 'James M. Sikes', 'address' => 'Brainerd, MN 56401' ] ]; |
Using Different Kinds of Loops in PHP With Example
So, the data I have prepared above is the data that we are mostly going to use inside our application and also the real time scenarios. We are going to use each type of them and see how to iterate over them in each of different kinds of loops in PHP.
1. Foreach Loop
For each loop is the most common loop used by the developers in the Realtime project then, comes rest of the loop so, make sure to know how its syntax works below.
foreach($array_string as $val)
where $array_string
is the array of data and $val
is the single value that iterates one by one inside the loop. Let’s take an example.
1. Looping over First Data ($array_string)
1 2 3 4 5 6 |
foreach($array_string as $stringValue): echo $stringValue . ', '; endforeach; // Output // 'Elias P Bone', 'Mary J Washington', 'Kevin S Wheeler', 'Donald J Mc Daniel', 'Paula P Cruz' |
If you have noticed that I have used :
& endforeach;
at the end of ending of PHP. Most of the developers don’t use these syntax but its very clear to understand rather than using curly braces.
It goes similar for other loops as well. You can also use foreach(...) {...}
if you get confused while using this.
2. Looping over Second Data (object_data)
1 2 3 4 5 6 |
foreach($object_data as $val): echo $val . ', '; endforeach; //outputs // Elias P Bone, 3889 Hurry Street, 010012000010101, someonesemail@email.com |
Another way of simply accessing the array of key value pairs by using their index. For this data it would be $object_data['name'], $object_data['address']
and so on. It will output the value of that key.
But, when you fetch the data from database, it comes, in the form of object which you need to access by using the arrow sign(->)
. For example you can convert this data into object by prefixing with (object)
in front of array like (object) $object_data
.
To access the data inside object all you have to do is $object_data->name
& $object_data->address
as so on. If you try to access the data of object by using big brackets like above you will encounter an error and vice versa. However foreach loop will iterate over object by using its key as an index.
To echo out index
in your for each loop just have to change a syntax a little bit like below.
1 2 3 |
foreach($object_data as $index => $val): echo $val; endforeach; |
Try echoing index and see it on your screen. I hope you have got the concept of array and object. So I am just going to demonstrate the rest of types of data in the form of array.
3. Looping over Second Data (object_data)
1 2 3 4 |
foreach($array_object_data as $data): echo $data['name']. ' ' . $data['address']. '<br>'; // echo $data->name. ' ' . $data->address. '<br>'; // incase of object endforeach; |
2. For Loop
For loop is the most oldest method of looping over the data in the programming. The first time you learn programming will start with for loop. To run the loop you will initialize the value and length of data. Check below the tutorial.
1. Looping over Normal Array of Strings.
1 2 3 |
for($i=0; $i < count($array_string); $i++): echo $array_string[$i]; endfor; |
2. Looping over Object
1 2 3 4 |
$keys = array_keys((array) $object_data); for($i=0; $i < count($object_data); $i++): echo $object_data[$keys[$i]]; endfor; |
3. Looping over Array of Key Value Pairs
1 2 3 |
for($i=0; $i < count($array_object_data); $i++): echo $array_object_data[$i]['name'] . ' ' .$array_object_data[$i]['address'] .'<br>'; endfor; |
3. While Loop
While loop is the loop which first evaluates the condition and defines the iteration to run or not. The iterator is started from zero by counting the length of the data you are going to integrate. Check the example below which will help you get clear more.
1. Looping over first Data
1 2 3 4 5 |
$iterator = 0; while($iterator < count($array_string)): echo $array_string[$iterator]; $iterator++; endWhile; |
2. Looping over second data
1 2 3 4 5 6 |
$iterator = 0; $keys = array_keys((array) $object_data); while($iterator < count($keys)): echo $object_data[$keys[$iterator]] . ' '; $iterator++; endWhile; |
3. looping over third data
1 2 3 4 5 |
$iterator = 0; while($iterator < count($array_object_data)): echo $array_object_data[$iterator]['name'] . ' ' . $array_object_data[$iterator]['address']; $iterator++; endWhile; |
4. Do While Loop
Do while loop is the condition where first step is executed once and then condition is checked if it should run next time or not. In this do while loop tutorial we are going to loop over all three types of data and see what can be the use case.
1. Looping over first data (Array of strings)
1 2 3 4 5 |
$doIterator = 0; do { echo $array_string[$doIterator]; $doIterator++; } while($doIterator < count($array_string)); |
2. Looping over second data (array of key value pairs)
1 2 3 4 5 6 |
$doIterator = 0; $keys = array_keys((array) $object_data); do { echo $object_data[$keys[$doIterator]]; $doIterator++; } while($doIterator < count($keys)); |
3. Looping over third data (array of key value)
1 2 3 4 5 |
$doIterator = 0; do { echo $array_object_data[$doIterator]['name'] . ' ' . $array_object_data[$doIterator]['address']; $doIterator++; } while($doIterator < count($array_object_data)); |
So, that is it for PHP loops tutorial with example. If you are confused and have problem iterating over you data make sure to contact us via contact form or leave comments below.
Leave a Reply