for loop vs while loop vs foreach loop PHP

后端 未结 6 1713
迷失自我
迷失自我 2020-11-29 09:29

1st off I\'m new to PHP. I have been using for loop,while loop,foreach loop in scripts. I wonder

  • which one is better for performance?
  • what\'s the cri
6条回答
  •  清酒与你
    2020-11-29 09:50

    • for loop is more appropriate when you know in advance how many iterations to perform
    • While loop is used in the opposite case(when you don't know how many iterations are needed)
    • For-Each loop is best when you have to iterate over collections.

    To the best of my knowledge, there is little to no performance difference between while loop and for loop i don't know about the for-each loop

提交回复
热议问题