for loop

PHP for loop is the most complex loops in PHP. They behave like their C counterparts.

Syntax of PHP for loop:

Following is given a general syntax of for loop in PHP:

In the beginning, loop condition is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed.
If it evaluates to FALSE, the execution of the loop ends.

All three expression, initialization, condition, increment/decrement can be empty or multiple.
If the condition is empty then loop will execute infinite considering it is true.
If the expressions are multiple then they are separated by commas.
 

Flowchart of PHP for loop

php-for-loop
 

Example
Consider the following examples.

Output:

Ankit Wasankar

Senior Java Developer
Java 11 | Microservices | Reactjs | SQL

Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.