PHP Loop with Example

PHP Loop with Example


A Loop is an Iterative Control Structure which requires a number of times until a given condition is met to perform the same number of codes. Sometimes when you write code, you would like a number of times the block of code to run repeatedly. So, we can use loops rather than applying almost identical code lines to a script.

Loops are used again and again to run the same block of code, provided a certain requirement is fulfilled. The fundamental concept behind a loop is to simplify repeated processes in a programmer so as to save time and effort.

A flow chart explain how loops working in PHP:

In PHP there are following types Loops:

  1. While Loop-Loops through a code block as well as given condition is true.
  2. Do..while Loop-The code block running once and then is tested state.if the conditon is true the declaration shall be repeated until the condition stated is true.
  3. For Loop-Loops via code block before a given number is reached in the counter,
  4. ForEach Loop - Loops of each unit of array across a block of code.