PHP FOR & FOREACH LOOP STATEMENTS - PHP LESSON 11 STUDENTSBLOG100

For Loop:
For loop will be executed if the condition given initially is true. The first part of for loop is for initialization, second is for declaring condition and third part is for Increment/Decrement.

Syntax:

Example:

Output:
Number is - 0
Number is - 1
Number is - 2
Number is - 3
Number is - 4
Number is - 5

Foreach Loop:
The foreach statement is used to loop through arrays and objects.When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. That is that we don't need to call reset() before a foreach loop.

In the example program, for every loop iteration, the value of the current array 'a's values  are assigned to $val and the array pointer is moved by one, until it reaches the last array element.

Syntax:

Example:

Output:
Values are 1
Values are 2
Values are 3
Values are 4
Values are 5

Lesson 10  Previous | Main Page | Next Lesson 12

Previous
Next Post »