PHP for Loops

PHP for loops execute a block of code a specified number of times.

  • for - loops through a block of code a specified number of times
  • foreach - loops through a block of code for each element in an array

PHP for Loop

for (init counter; test counter; increment counter) {
    code to be executed;
}

PHP foreach Loop

The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

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