Comparing a while loop against a foreach loop of an array

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!