PHP Looping (Cont.)
The for
Statement (Cont.)
The for statement has three parameters:
- the first parameter initializes variables,
- the second parameter holds the condition, and
- the third parameter contains the increments required for the loop.
If more than one variable is included in the initialization or the increment parameter, they should be separated by commas.
The condition must evaluate to true or false.
The foreach Statement
The foreach statement is used to loop through arrays.
|
|
foreach ( array as value ) {
code to be executed; }
|
|
For every loop, the value of the current array element is assigned to $value (and the array pointer is moved by one)—so on the next loop, you’ll be looking at the next element.
|
|
|
The array([mixed $...])
creates an array and returns an array of the parameters.
The parameters can be given an index with the =>
operator, whose syntax index => values
defines index and values, e.g.,
$class = array( 0 => "Freshman", 1 => "Sophomore",
2 => "Junior", 3 => "Senior" );