Differences between a while loop and a for loop in PHP?

前端 未结 6 2065
梦谈多话
梦谈多话 2020-12-06 12:02

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.<

6条回答
  •  温柔的废话
    2020-12-06 12:19

    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.

提交回复
热议问题