For Loop:
Output:
Foreach 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:
Number is - 0
Number is - 1
Number is - 2
Number is - 3
Number is - 4
Number is - 5
Number is - 1
Number is - 2
Number is - 3
Number is - 4
Number is - 5
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:
Values are 1
Values are 2
Values are 3
Values are 4
Values are 5
Values are 2
Values are 3
Values are 4
Values are 5
No comments:
Post a Comment