I\'m reading an ebook on PHP right now, and the author noted that the difference between a while loop and a for loop is that the for loop will count how many times it runs.<
It's a matter of taste, personal preference and readability. Sometimes a while
loop works better logically. Sometimes, a for
.
For my personal rule, if I don't need a variable initializer, then I use a while
.
But a foreach
loop is useful in its own way.
Plus, in the case of PHP's scoping, where all variables not inside of functions are global, it the variable will continue living after the loop no matter which loop control you use.