Foreach Loop In Php

  1. Upgrade your Clever Techie learning experience: UPDATE! (9/13/19) New features and improvements for Clever Techie Patreo.
  2. Foreach loop (or for each loop) is a control flow statement for traversing items in a collection.Foreach is usually used in place of a standard for loop statement.Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say 'do this to everything in this set', rather than 'do this x times'.
  • PHP Tutorial
  • Advanced PHP
  • PHP Form Examples

For-each is another array traversing technique like for loop, while loop, do-while loop introduced in Java5. It starts with the keyword for like a normal for-loop. Instead of declaring and initializing a loop counter variable, you declare a variable that is the same type as the base type of the array, followed by a colon, which is then followed.

  • PHP login Examples
  • PHP AJAX Examples
  • PHP XML Example
  • PHP Frame Works
  • PHP Design Patterns
  • PHP Function Reference
  • PHP Useful Resources
  • Selected Reading

Loops in PHP are used to execute the same block of code a specified number of times. PHP supports following four loop types.

  • for − loops through a block of code a specified number of times.

  • while − loops through a block of code if and as long as a specified condition is true.

  • do..while − loops through a block of code once, and then repeats the loop as long as a special condition is true.

  • foreach − loops through a block of code for each element in an array.

Foreach Loop In Php Table

We will discuss about continue and break keywords used to control the loops execution.

The for loop statement

The for statement is used when you know how many times you want to execute a statement or a block of statements.

Syntax

The initializer is used to set the start value for the counter of the number of loop iterations. A variable may be declared here for this purpose and it is traditional to name it $i.

Example

The following example makes five iterations and changes the assigned value of two variables on each pass of the loop −

This will produce the following result −

Digital music users largely fall into two camps these days: those with their own, personal, curated music collection and others who use a music streaming service. The former group often runs into a dilemma: How do we keep discovering quality new music? How do we avoid losing ourselves, and our curated albums, in an infinite sea of (streaming) music? To help provide a bridge between those two worlds, we’ve par. Tidal and plex login. Purchasing a TIDAL subscription through Plex gives you access to TIDAL both in and outside of Plex. Do I need a Plex Pass to get TIDAL? No, regardless of which subscription you choose, you get access to all of TIDAL’s 60 million tracks, 250,000+ music videos, exclusive content, PLUS some super awesome audio features from Plex. With a TIDAL x Plex subscription, visit any artist in your own (beautifully Plex-organized) library and see albums you’re missing magically appear along with their music videos. Or get recommended artists.

The while loop statement

Key

The while statement will execute a block of code if and as long as a test expression is true.

If the test expression is true then the code block will be executed. After the code has executed the test expression will again be evaluated and the loop will continue until the test expression is found to be false.

Syntax

Example

This example decrements a variable value on each iteration of the loop and the counter increments until it reaches 10 when the evaluation is false and the loop ends.

This will produce the following result −

The do..while loop statement

The do..while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true.

Syntax

Example

The following example will increment the value of i at least once, and it will continue incrementing the variable i as long as it has a value of less than 10 −

This will produce the following result −

The foreach loop statement

Php foreach array as key value

The foreach statement is used to loop through arrays. For each pass the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed.

Syntax

Example

Try out following example to list out the values of an array.

This will produce the following result −

The break statement

The PHP break keyword is used to terminate the execution of a loop prematurely.

The break statement is situated inside the statement block. It gives you full control and whenever you want to exit from the loop you can come out. After coming out of a loop immediate statement to the loop will be executed.

For Loop Php

Example

In the following example condition test becomes true when the counter value reaches 3 and loop terminates.

C# foreach example

This will produce the following result −

The continue statement

The PHP continue keyword is used to halt the current iteration of a loop but it does not terminate the loop.

Just like the break statement the continue statement is situated inside the statement block containing the code that the loop executes, preceded by a conditional test. For the pass encountering continue statement, rest of the loop code is skipped and next pass starts.

Example

Foreach Loop In Php

In the following example loop prints the value of array but for which condition becomes true it just skip the code and next value is printed.

Foreach Loop In Php Example

This will produce the following result −