For loop in PHP :

The for loop is the most potent loop, it combines the abilities to set up variables as we enter the loop, test for conditions while iterating loops, and it modifies variables after each iteration.

Example:

Output:

In the above example, we are outputting the 10 times table from a for loop.

Each for statement takes three parameters:

  • An initialization expression.
  • A condition expression.
  • A modification expression.

These three parameters are separated by semicolons, for (exp1;exp2; exp3).

  1. The start of the first iteration of the loop, the initialization expression is executed.
  2. Each time around the loop, the condition expression is tested, and the loop is entered only if the condition is TRUE.
  3. At the end of each iteration,the modification expression is executed.

Example (Even Numbers from 1 to 100):

Output:

  • In the above example, for loop from ($i=2) and after every count, $i increment its value by 2, which print all the even value from 1 to 100.

Example (Pattern1):

Output:

Example (Pattern2):

Output:

Example (Pattern3):

Output:

Example (Pattern4):

Output:

Example (Pattern5):

Output:

Example (Pattern6):

Output:

Example (Pattern7)

Output:

Example (Table using for loop):

Output:
PHP for Loop Example

Pin It on Pinterest

Share This