PHP Array Functions and C Syntax Loop Methods Few People Even Know.
Note, sorry but this article is going to skip between languages a bit.
Everyone knows about count
, for
and foreach
. They’re the staple of programming array loops. If you work in PHP, you likely know what they do and how to use them…
But what aboutcurrent
, end
, key
, last
, next
, prev
, and reset
?
Much less how if
/do
/while
can be used to smartly make loops in most all C syntax languages?
I’ve been shocked the past year or so how many dev’s I come across who’ve never even heard of them, much less understand their use. What prompted this article is that I was just working on a “function stack” of anonymous that other processes can add to, and I used this because of the “You can’t add to an array inside a foreach” issue; and a colleague saw it and said “what the devil is that?!?” Actually he used a bit stronger language when he saw the do/while.
They are an alternative way of going through an array — and in some cases objects — based on the simple fact that:
PHP Array’s… Aren’t.
That might sound odd, but from the perspective of an assembly language, C, or Pascal/ADA developer like myself, it’s true. PHP fakes the APPEARANCE of arrays just like JavaScript does.