Recently I asked the application architect at work to consider the php script that I wrote to automate some of the tasks that I perform weekly in our support department.
In his review, he stated
## Loops
Your loops are good, you didn't do anything bad like calling functions in the condition
for ($i=0; $i < count($array); $i++); is BAD
Honestly, I was never tempted to do this in my code before, but it made me wonder why it would be bad.
I guess this is because the result of the function can be any value at all, and it seems like the perfect way to create an infinite loop and just generally causes unexpected behavior.
I tried a search on Google but could not find any suitable results, so I ask:
Why is it bad to call a function in a for loop condition?
Note count($array) in the comment itself, for me, gimme. Of course, you just want to cache this. In particular, I mean the use of other more complex functions.
For those who are probably wondering: โWhy not just ask the guy who wrote it,โ he is very busy and has already taken the time to help me, I donโt want to push too much โNow please explain all your comments to me?โ
source share