Jump to content

For/while loops


jimfog

Recommended Posts

I am new to php and my question will be considered rudimentary.When looping arrays is there any difference in using a for or a while loop or it;s just a matter of personal preference?Thanks.

Link to comment
Share on other sites

If you plan on iterating over every item in the array, a for-loop is probably simplest. If you plan to break out of the loop when a condition arises, it depends on how you want to break. A while loop assumes that you want to execute every statement in the block before terminating the loop. With a for-loop, you might want to break out if one or more conditions arise anywhere in the block. Obviously, you can break out of a while-loop anywhere, also. But choosing one or the other lets a developer glance at the loop and make guesses about the job it's doing without having to read all the statements in the block.Performance-wise, there is no real difference until we're talking about many thousands of statements being executed.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...