Mar 02 2009

Try end() next() time

Here’s a short tip today. I’ve been finding that when using foreach in PHP to check if there’s more in the array, the use of next() has failed me on multiple occasions . I just converted every instance of next() in my code to instead use end().

if($cat !== end($categories)) { echo ", "}

This way, I don’t need to worry about the internal pointer being pushed around before the loop, or during the loop. I really only care if the value I’m abusing is the last value, anyways.

  • #php