As taken from https://stackoverflow.com/questions/4891301/top-bad-practices-in-php
Is this similar code killing kittens, too?
foreach (file("longFile.txt") as $line) {
// ouch! Kitten killed
}
??
For those who have no idea what am I talking about:
Is PHP getting longFile.txt everytime it goes to next line of file or no? Talking about this code:
foreach (file("longFile.txt") as $line) {
// something
}
In the linked question the
forloop incurs a performance hit by callingcounton every iteration.foreachuses internal pointers to iterate through the array passed to it.In your example
file()will be called once and the resulting array will be passed toforeachwhich will iterate through it, thus the kittens are saved. Happy Caturday!