DEV Community

Cover image for Loops on the Go
Yusuf Saifurahman
Yusuf Saifurahman

Posted on

Loops on the Go

LOOPS

What are loops :

Loops: Execute code a certain number of times.
Loops are used to execute the same block of code again and again, as long as a certain condition is met. The basic idea behind a loop is to automate the repetitive tasks within a program to save time and effort. PHP supports four different types of loops which are:

1.For loop

2.For each loop

3.While loop

4.Do while loop

For Loop:

Loops through a block of code until the counter reaches a specified number. Basically, a “For Loop” is used when you know the exact number of times.

A for loop is expected to the following for it to be complete:(parameter - Initializer, Condition, Increment)
Alt Text

1.“for…{…}” is the loop block
2.initialization — it is used to initialize the counter variables and evaluated once unconditionally before the first execution of the body of the loop.
3.condition — at the beginning of each iteration, the condition is evaluated. If it evaluates to true, the loop continues and the nested statements are executed. If it evaluates to false, the execution of the loop ends.
4.increment — it updates the loop counter with a new value. It is evaluated at the end of each iteration.

How the FOR LOOP works:
Alt Text
Let’s put that into an example and see what it outputs:
The example below defines a loop that starts with $i=1. The loop will be continued until $i is less than or equal to 10. The variable $i will increase by 1 each time the loop runs:
Alt Text

For each loop:-

This is different from other types because arrays are involved. loops through a block of code for each element in an array.

Alt Text
1.foreach(…){…}” is the foreach PHP loop block code
2.$array_data” is the array variable to be looped through
3.$array_value “ is the temporary variable that holds the current array item values.
4.block of code…” is the piece of code that operates on the array values
How the FOR EACH LOOP works:
Alt Text

The example below portray a loop that will output values of the given array:
But first what is an Array:
An array in PHP is actually an ordered map. A map is a type that associates values with keys. This type is optimized for several different uses; it can be treated as an array, list (vector), a hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.
Alt Text
The foreach loop also has another style in which it can be written as:
Alt Text

With these let's look at an example to understand the block.
Alt Text

While loop:-

Used to transverse a set of code like for loop (Used over a for Loop if you don't know the number of times.) Or in other words, The while statement will loop through a block of code as long as the condition specified in the while statement evaluates to true.

When to use while loops

1.While loops are used to execute a block of code until a certain condition becomes true.
2.You can use a while loop to read records returned from a database query.
Alt Text
1.while(…){…}” is the while loop block code
2.condition” is the condition to be evaluated by the while loop
3.block of code…” is the code to be executed if the condition gets satisfied
How the WHILE LOOP works:
Alt Text

The code snippet below defines a loop that starts with $i=1. The loop will continue to run as long as $i is less than or equal to 10. The $i will increase by 1 each time the loop runs:
Alt Text

Do while loop:-

The do-while loop is a variant of the while loop, which evaluates the condition at the end of each loop iteration. With a do-while loop, the block of code executed once, and then the condition is evaluated, if the condition is true, the statement is repeated as long as the specified condition evaluated to is true. Not Commonly Used, but Conditions are passed on the while part.
Alt Text
1.do{…} while(…)” is the do… while loop block code
2.condition” is the condition to be evaluated by the while loop
3.block of code…” is the code that is executed at least once by the do… while loop
Alt Text
The following example defines a loop that starts with $i=1. It will then increase $i with 1, and print the output. Then the condition is evaluated, and the loop will continue to run as long as $i is less than, or equal to 10.
Alt Text

Summary

1.The for… loop is used to execute a block of a specified number of times
2.The foreach… loop is used to loop through arrays
3.The While… loop is used to execute a block of code as long as the set condition is made to be false
4.The do… while loop is used to execute the block of code at least once then the rest of the execution is dependent on the evaluation of the set condition

Difference Between while and do…while Loop

The while loop differs from the do-while loop in one important way — with a while loop, the condition to be evaluated is tested at the beginning of each loop iteration, so if the conditional expression evaluates to false, the loop will never be executed.
While a do-while loop, on the other hand, the loop will always be executed once, even if the conditional expression is false because the condition is evaluated at the end of the loop iteration rather than the beginning.

At this point, you must have learned a lot about PHP loops and must be quite comfortable working with them. Should you want to learn more, read the official documentation here: https://www.php.net/docs.php

Thanks for taking your time to read this...
you can catch me on Twitter: @yusuf_software

Top comments (0)