You likely learned about loop constructs very early in your programming career and use them every day--so often you don’t think twice! The problem is for() and foreach() are so fundamental developers use them for everything! Even worse almost every developer will use them differently to accomplish the same goal. So what would intention-revealing PHP code look like without using for() and foreach()? Let’s explore some functional programming concepts and find out!

Knowing basic functional programming concepts will improve your PHP code. Your new mindset will favor writing code with better separation of concerns and decreased complexity: clean code. We will explore some poor examples of using loops to process data, then ease into learning functional concepts like map, reduce, filter and collection pipelining. We'll then use our newfound knowledge to refactor a large foreach() loop to use the new techniques. Lastly we’ll explore some existing libraries and even experimental PHP RFC features to make our code even more concise. Beware: after this talk you'll never look at for() and foreach() loops the same again!


Comments are closed.