PHP For Loops

PHP for loop tutorial explain you How to use PHP For loop? with simple examples.

The expression initialization is assessed once i.e. at the starting of the for loop. Each time through the loop, the expression condition is tested. Assuming that it is true, the body of the loop executed; provided that it is false, the loop ends. The expression increment is assessed each time after the loop form runs.

Syntax for PHP For loop:

PHP For loop takes three expressions inside its parentheses, split by semi-colons. The point when PHP For loop executes, this happens:

  1. The initializing expression is executed. This expression ordinarily initializes one or more loop counters, yet the syntax permits an expression of any level of complexity.
  2. The condition expression is assessed. Assuming that the value of condition is true, the loop statements execute. Provided that the value of condition is false, the For loop terminates.
  3. The update expression increment executes.
  4. The loop execute, and control comes back to step 2.

Example on PHP FOR loop:

Output will be:

PHP Foreach loop

The PHP Foreach loop is a variation of the For loop and allows you to iterate over elements in an array.
There are two different versions of the Foreach loop.

Foreach loop syntax are as follows:

Example on PHP For each loop:

Output will be:

PHP executes the body of the loop once for every element of $email in turn, with $value set to the present element. Elements are handled by their inside request. Looping proceeds until the Foreach loop achieves the final elementor upper bound of the given array.

An alternative form of PHP Foreach loop gives you access to the current key:

Output will be:

SHARE
Previous articlePHP While Loop
Next articlePHP Functions
I am a young Information Systems Security Engineer(CEH). I had started with penetration testing when i am 17 years old. It all started with Kali linux and Metasploit.In free time i will play with Security holes/Bug bounty/Pentesting and Hacking !!!