Articles about while

Helping you solve those tough coding problems since 2009!

A Short Rant About Coding Conventions

Published on Sep 13, 2012

Yesterday's article actually got me a little amped up about coding conventions – Comparing a while loop against a foreach loop of an array – because I never thought I would actually have to do a comparison between a while loop and a foreach loop on an array!  If we go back and revisit the post, I was reviewing a recent CakePHP commit for an optimization on the Hash class.  The code in question is three separate blocks of code that leverage the array_shift function to get the next value in the array with a foreach loop instead.

Comparing a while loop against a foreach loop of an array

Published on Sep 12, 2012

Are you confused by the title?  I was when I first got the idea to even write this blog as well.  I was recently perusing the CakePHP change logs and came across an interesting commit – Optimization for Hash Method.

The optimization is quite simple, this code:



while (($key = array_shift($parts)) !== null)

Is replaced with the following code:



foreach ($parts as $key)

This is actually done several times in the commit.  It seems that the original developer is really keen on using the array_shift function.  Just in seeing this code, I thought the original code was odd to utilize that function so I immediately had to do a comparison and validate the optimization!

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.