* fix: replace sh with shell fix replace terminal with shell fix replace node with js fix replace output with shell fix replace cs with csharp fix replace c++ with cpp fix replace c# with csharp fix replace javasctipt with js fix replace syntax with js fix replace unix with shell fix replace linux with shell fix replace java 8 with java fix replace swift4 with swift fix replace react.js with jsx fix replace javascriot with js fix replace javacsript with js fix replace c++ - with cpp fix: corrected various typos fix: replace Algorithm with nothing fix: replace xaml with xml fix: replace solidity with nothing fix: replace c++ with cpp fix: replace txt with shell fix: replace code with json and css fix: replace console with shell
2.2 KiB
title
title |
---|
For Loop |
For Loop
The PHP for
statement consists of three expressions and a statement:
for ((initialization); (condition); (final-expression)) statement
Description
- initialization
- Run before the first execution on the loop.
- This expression is commonly used to create counters.
- Variables created here are scoped to the loop. Once the loop has finished it is execution they are destroyed.
- condition
- Expression that is checked prior to the execution of every iteration.
- If omitted this expression evaluates to
true
.
- final-expression
- Expression that is run after every iteration.
- Usually used to increment a counter.
- But it can be used to run any expression.
- statement
- Code to be repeated in every loop iteration.
Any of these three expressions or the statement can be ommited.
The expressions can contain multiple expressions separated by comma.
In the (condition) expression, all the comma separated expressions will be evaluated.
The result is obtained from the last one.
For loops are commonly used to count a certain number of iterations to repeat a statement.
Common Pitfalls
Exceeding the bounds of an array
When indexing over an array many times it is easy to exceed the bounds of the array (ex. try to reference the 4th element of a 3 element array).
// This will cause an error.
// The bounds of the array will be exceeded.
$arr = array(1,2,3);
for ($i = 0; $i <= count($arr); $i++) {
var_dump($arr[$i]);
}
This will output:
int(1) int(2) int(3) NULL
There are to ways to fix this code.
Set the condition to either $i < count($arr)
or $i <= count($arr) - 1
.
Performance Issues
The above code can became slow, because the array size is fetched in every iteration.
In order to fix this problem it is possible to put the array size into a variable.
//create the $size variable with a second expression comma separated
for ($i = 0, $size = count($arr); $i < $size; ++$i) {