Tutorial for “For each” loop

What is For each Loops?

Use the for each loops to cycle through arrays. At each pass, the value of the current array element is set to $value. In the next pass, the array pointer is moved one by one. The for each loop is only used to loop around each key/values pair in an array. Only arrays are supported.

Syntax:-

foreach ($array as $value) {
  code to be executed;
}

Here are some of the Foreach Loops:-

  • “foreach(…){…}” is the foreach php loop block code
  • “$array_data” is the array variable to be looped through
  • “$array_value “ is the temporary variable that holds the current array item values.
  • block of code…” is the piece of code that operates on the array values
PHP Loop and  Control Structures

Example1:-

Output:-

Example2:-

Output:-

Ashwani Kumar
Latest posts by Ashwani Kumar (see all)
Tagged : / / / / / / / / / /
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x