#7: PHP Tutorials for Beginners – Loops

CodingIn the last post, we learnt about the first kind of control structures which are, if statements which allowed us to run a block of code if a condition is true or not. But what about writing a code which would run a block of code again and again? That’s where we will need loops for. Loops allow us to execute a block of code again and again if a condition is true.

In this post, we will talk about different kinds of loops like – While Loop, Do-while Loop, For Loop and For-each Loop. We will also talk about – Continue, Break and Pointers.

While Loop

While loops are used to execute a block of code till a specified condition is true. The format of a while loop is:

while (expression)
statement;

The format of While Loop is much like the if statements. You may see a sample code here and its online demo here.

Do-while Loop

Do-while loops are much like the while loops. This loop executes the block of code atleast once and then executes it again only if the specified condition is true. The format of the do-while loop is:

do {
statement;
} while (expression);

You may see a sample code here and its online demo here.

For loop

For loops execute a block of code a specified number of times. They loop through the same way like the While Loops where the code is run when a condition is true but the syntax is a little different. Some may find this easy who have programming experience with other languages like C. The format of for loop is:

for (expression1, expression2, expression3) {
statement;

The first expression is evaluated at the very beginning of the loop for the very first time through the loop only. It’s like an initializing statement which happens only once. The second expression is an evaluated expression that happens at the beginning of every loop. That is what we had in the while loop before. The third one is what happens at the end of each and every loop.

You may read the 3 expressions as initial (this is, the initial value we are giving, we are initializing something), test (it is the test which we perform each time and as long it is true, it will continue the loop) and each (each is the action we perform at the end of every loop before looping back to the top and doing the test again).

You may see a sample code here and its online demo here.

Foreach Loop

Foreach loops are used to execute a block of code for each element in an array. The format of Foreach Loop is:

foreach ($array as $value)
statement;

This loop is different from other loops. In other loops, we are always testing a boolean whether it is true or not. Foreach Loops only works on arrays and the boolean test in Foreach Loops is do we have items still left in an array. So Foreach Loop will go through every single element of the array. Each of those elements in the array will become the value and the value can be referenced in the statement.

You may see a sample code here and its online demo here.

Continue

As we have learnt about loops, there are some more options that you should know. One of them is continue. You may see a sample code here for explanation on continue. The online demo for the same is here.

Break

Break ends the execution of the loop. After which, PHP skips the loop then and there and jumps to the next task it has to do. You may see a sample code here for more explanation. The online demo for the same is here.

Pointers

When you have an array in a PHP file, PHP maintains a pointer that points to the current value of an array and by default it’s only the first value. And, when we start looping through an array, PHP moves that pointer along the array to get each value and to keep a track on which value it has already used in the array. You may see a sample code here for more explanation. The online demo for the same is here.

Hope you enjoyed the tutorial! Please spill your comments/suggestions/feedback/queries in the comments section :-). In the next post, we will learn about user defined functions.

(Image Credits: S.V Meditrans, Inc.)