Loops on the Go
Tags: php, webdev, simplified
Published: 2021-04-30 06:32:54

#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
<br>


##__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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mit8pee6z5uq20zc6oyl.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5uc263t984gz6wdsfvzd.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jv7wh5wmvlpzdshihkq.png)

##__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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mwoa9qf5k5a4d626fekm.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5mb1byjke9iltq0m4tv.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xlxj1zz6euc5cilautu8.png)
The foreach loop also has another style in which it can be written as:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ad3fk1tti922akh9g529.png)

With these let's look at an example to understand the block.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7u7e1e017u23mkml6y38.png)
##__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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fosq4tvw3roc4pve4fhu.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhxnyy5u171vega7kov9.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azypnqbgo43vbqtj5bhh.png)
##__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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/311w0l1f4w8bh9ovourj.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b792xgnjfgrj8o646l1v.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23ux01rvvepzpgotvq1i.png)
#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