Differences between while loop and for loop in PHP?

Now I am reading an e-book in PHP, and the author noted that the difference between the while loop and the for loop is that the for loop will count how many times it starts.

So take this:

<?php for ($i = 1; $i < 10; $i = $i + 1) { print "Number $i\n"; } ?> 

But won't it be the same as

 <?php $i = 1; while ($i < 10) { $i = $i + 1; print "Number $i\n"; } ?> 

Or are there other differences that he did not indicate? (Besides using a while loop, when you don't know how long the condition will remain true, for example, selecting rows from the database)

I mean, if this is the only difference, can't I just not use the for loop and use the while loop instead?

+8
php for-loop while-loop
source share
5 answers

You can? Oh sure. But should you decide a completely different question.

The for loop is more readable in this scenario, and this is definitely the convention you'll find in it in almost every language that has loop directives. If you use a while , people wonder why you haven't used a for loop.

+10
source share

"For" more clearly expresses your intentions

Functionally, your two examples are the same. But they express different intentions.

  • while means "I don't know how long this condition will last, but as long as it does, do it."
  • for means "I have a certain number of repetitions to execute."

You can use it when you mean another, but it is more difficult to read the code.

Other reasons are preferred here for why for here

  • It is more concise and puts all the loop information in one place.
  • It makes $i local variable for the loop

Don't forget foreach

Personally, the loop that I use most often in PHP is foreach . If you find yourself doing things like this:

 for ($i=0; $i < count($some_array); $i++){ echo $some_array[$i]; } 

... then try the following:

 foreach ($some_array as $item){ echo $item; } 

Faster to enter, easier to read.

+11
source share

Functionally, a for loop is equivalent to a while ; that is, each can be rewritten as another, without changing the result or side effects. However, each of them has different connotations. A while loop is executed during the execution of the condition; the condition is static, although circumstances change. The for loop executes on a sequence. The difference is important for programmers, but not for programs, since the choice of variable names is important for programmers, even if they can be changed to create functionally equivalent code. One loop design will make more sense than another, depending on the situation.

+4
source share

A for cycle

 for (INIT; CONDITIONS; UPDATE) { BODY } 

basically the same as a while loop, structured as follows:

 INIT while (CONDITIONS) { BODY UPDATE } 

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

+3
source share

It is a matter of taste, personal preference and readability. Sometimes a while works better logically. Sometimes a for .

For my personal rule, if I do not need a variable initializer, I use while .

But the foreach is useful in its own way.

In addition, if you define a PHP area where all variables that are not part of the function are global, the variable will continue to live after the loop, regardless of which loop control you use.

0
source share