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
startis evaluated once when the loop starts. - The
conditionis evaluated once in each iteration. If theconditionistrue, thestatementin the body is executed. Otherwise, the loop ends. - The
incrementexpression 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: