PHP for

Summary: in this tutorial, you will learn about PHP for statement to execute a block of code repeatedly.

Introduction to PHP for statement #

The for statement allows you to execute a code block repeatedly. The syntax of the for statement is as follows:

<?php

for (start; condition; increment) {
	statement;
}Code language: PHP (php)

How it works.

  • The start is evaluated once when the loop starts.
  • The condition is evaluated once in each iteration. If the condition is true, the statement in the body is executed. Otherwise, the loop ends.
  • The increment expression is evaluated once after each iteration.

PHP allows you to specify multiple expressions in the start, condition, and increment of the for statement.

In addition, you can leave the start, condition, and increment empty, indicating that PHP should do nothing for that phase.

The following flowchart illustrates how the for statement works: