PHP Classes

You have doing PHP iteration the wrong way!

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog You have doing PHP it...   Post a comment Post a comment   See comments See comments (10)   Trackbacks (0)  

Author:

Viewers: 2,092

Last month viewers: 1

Categories: PHP opinions

Never loop through a problem that should be solved by iteration.

Read this opinion article to understand why using iterators is a better idea than using loops in PHP.




Loaded Article

Foreach, for and while loops have turned into the defacto standard for doing "iteration" in PHP, making it impossible for self-taught, "I'm a beginner in the PHP ecosystem" kind of programmers (or developers) to understand that they are inefficient "iterators for every use-case" and are not even meant for iterating at all, they are just looping functions and that is what they should be used for, loops.

Recently, I was going through a program written by somebody I know. He needed to iterate over a multi dimensional array that contains over 100 entries to wrap each of those entries with brackets, he used a for loop inside a for loop and had a very lengthy run of 'his algorithm'.

That was bad. I wish he knew PHP has an `IteratorIterator` class specifically meant for doing this, I wish he knew theres even a more interesting solution, a `RecursiveIteratorIterator` class. I wish he knew there is a difference between looping through an array and iterating over it or even more better, I wish he knew there is a very huge difference between iteration and looping.

Most of the time, we get confused (or too lazy). We use loops for every use case and then keep wondering why the value of our php.ini max_execution_time variable has to be increased for PHP to stop showing those heartbreaking "fatal errors".

I have written this to establish a line in our minds, a line every PHP introductory material never draws.

Never loop through a problem that should be solved by Iteration.

I'd recommend to iterate (a little pun intended) over each of the links below, they might help to paint over the lines I've just drawn in your minds (at least I hope so) and make that line stay forever.




You need to be a registered user or login to post a comment

1,611,040 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

1. Loops are not equivalent to Iterators - Stefan Jibrail Froelich (2016-10-12 21:29)
Poorly written, inaccurate and misleading... - 3 replies
Read the whole comment and replies

2. Sure, but performance.. - Vlad (2016-10-12 20:43)
Performance... - 2 replies
Read the whole comment and replies

5. I agree with you in general. - Oleg Zorin (2016-10-12 16:57)
Loops for arrays and iterators for other data structures.... - 0 replies
Read the whole comment and replies

4. Iterators vs Loops - Jimmy Thrasher (2016-10-12 16:57)
Misleading article... - 0 replies
Read the whole comment and replies

3. Wrong information - Arun K (2016-10-12 16:57)
Poor quality post... - 0 replies
Read the whole comment and replies



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog You have doing PHP it...   Post a comment Post a comment   See comments See comments (10)   Trackbacks (0)