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

前端 未结 6 2070
梦谈多话
梦谈多话 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条回答
  •  萌比男神i
    2020-12-06 12:16

    A for-loop

    for (INIT; CONDITIONS; UPDATE) {
        BODY
    }
    

    is basically the same as a while-loop structured like this:

    INIT
    while (CONDITIONS) {
        BODY
        UPDATE
    }
    

    While you could technically use one or the other, there are situations where while works better than for and vice-versa.

提交回复
热议问题