When using a typical for
loop, PHPStorm suggests to change it for better performance. I don't really understand the suggestion. How should I change it?

- 30,738
- 21
- 105
- 131

- 11,498
- 17
- 86
- 139
-
2What's the PHPStorm suggestion when you click on **more** ? I'm curious why PHPStorm suggests it. – Samuel Dauzon Feb 01 '16 at 09:00
-
there's just `analyses for loops and reports the usage blablabla` :) – simPod Feb 01 '16 at 09:02
-
1`foreach` and forget – Kzqai Feb 01 '16 at 19:42
3 Answers
for($i = 0; $i <= count($data); $i++){}
In this example for every iteration it has to count($data)
again.
for($i = 0, $iMax = count($data); $i <= $iMax; $i++){}
In this example it only needs to count($data)
once.
That's the difference.

- 12,099
- 6
- 34
- 51
-
1For reference see [this post](http://stackoverflow.com/a/12847555/2865814) – T3 H40 Feb 01 '16 at 09:03
-
2Presumably this assumes that `$data` does not change length inside the loop, or the two would be different. – abligh Feb 01 '16 at 12:36
-
4@abligh If `$data` changes length inside the for-loop, you probably shouldn't be using a for-loop in the first place. I may be biased though, I don't think you should ever use a for-loop. – Odalrick Feb 01 '16 at 12:58
-
`$iMax` is a really bad name for a variable. It should be `$length` or something more readable. Also, using `foreach($array as $i => &$value)` will yeld the same results and can actually be as fast as your `for()` loop. – Ismael Miguel Feb 01 '16 at 13:01
-
-
1@Odalrick `for` loops have their uses. In OP's case however, a glance at the variable names suggests that a `foreach` *may* be a better idea. I'm often strongly in favor of `foreach` anyway, since it doesn't care about arrays/structures with gaps in them and (IMO) looks a little bit cleaner. – Feb 01 '16 at 14:53
-
@Maurycy Because I don't like them. :) Mostly it's the syntax that annoys me: nowhere else can you put three statements inside a parethesis and have then do different things. In the specific instance where the length if the "iterated" value is changed however; you should make it explicit that you expect the lenght to change and a for-loop isn't somewhere the length is expected to change. As evidenced by the code insight that doesn't expect `count($data)` to change. – Odalrick Feb 01 '16 at 14:58
-
@IsmaelMiguel - actually, there's a subtle difference between the `for` and `foreach`. If you have an `array(0 => "a", 2 => "b", 3 => "c")` and use the `for` loop, `$i` will be in the sequence `0, 1, 2` - `foreach` will give you `0, 2, 3` - of course, it all depends on what you're doing with`$i` in the loop – HorusKol Feb 01 '16 at 23:06
-
But your `for` will throw warnings like `Unknown key 1 on line X` and will never reach the key `3`. – Ismael Miguel Feb 01 '16 at 23:09
If you execute the count()
inside your for
loop, then it's executed every iteration, and calling the function is a performance overhead.
If instead you call count()
before the for
loop and assign the result to a variable, then compare against the variable in the for
loop, you don't haver the function call overhead, so it's faster

- 209,507
- 32
- 346
- 385
By having the loop in the manner you do, each iteration it needs to evaluate count($data)
. If you've got a lot of items in the list, it could take a while (relatively) to count those items, and it has to do it each time.
The hint it is diving you is to set a variable to the value of count($data) at the beginning and then use that variable as the loop limit, when it's only evaluated once.
This can be done in two ways:
$loopMax = count($data);
for ($i = 0; $i <= $loopMax; $i++) {
// Your code here
}
Or
for ($i=0, $loopMax = count($data); $i <= $loopMax; $i++) {
// your code here
}

- 4,235
- 3
- 27
- 31
-
The top one is obviously preferred if you're going to iterate over multiple times for some reason. The bottom is preferred in other times because you should always strive to limit the scope of your variables. – corsiKa Feb 01 '16 at 22:47
-
The use of the top one isn't limited to when you're going to iterate over it several times, it would also be useful if the application, say, has a need to output the count; perhaps for some pagination or similar. But yes, scope of variables should be limited to where needed. – gabe3886 Feb 02 '16 at 08:58