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

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.

So take this:

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

But wouldn’t this be the same as

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

Or is there some other differences that he didn’t point out? (Aside from using while loop for when you’re unsure of how long the condition will remain true, such as selecting rows from a database)

I mean, if that’s the only difference, can’t I just not use the for loop and use the while loop instead?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

"For" expresses your intentions more clearly

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 this thing.’
  • for means ‘I have a specific number of repetitions for you to execute.’

You can use one when you mean the other, but it’s harder to read the code.

Some other reasons why for is preferable here

  • It’s more concise and puts all the information about the loop in one place
  • It makes $i a local variable for the loop

Don’t forget foreach

Personally, the loop 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 this:

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

Faster to type, easier to read.

Solution 2

Can you? Yes, certainly. But whether or not you should is an entirely different question.

The for loop is more readable in this scenario, and is definitely the convention you’ll find used within virtually every language that has looping directives. If you use the while loop, people are going to wonder why you didn’t use a for loop.

Solution 3

Functionally, a for loop is equivalent to a while loop; that is, each can be rewritten as the other with no change to the outcome or side effects. However, each has different connotations. A while loop runs while a condition holds; the condition is static, though circumstances change. A for loop runs over a sequence. The difference is important to programmers but not programs, just as choice of variables names are important to programmers even though they can be changed to produce functionally equivalent code. One loop construct will make more sense than the other, depending on the situation.

Solution 4

A for-loop


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

while (CONDITIONS) {

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

Solution 5

The Main Difference Between for() and While() is that we have to define the limit or count but in while() loop we don’t define limit or count it works until reached the last item

Initialization may be either in loop statement or outside the loop.
It is normally used when the number of iterations is known.
Condition is a relational expression.
It is used when initialization and increment is simple.
for ( init ; condition ; iteration )
{ statement(s); }

Initialization is always outside the loop.
It is normally used when the number of iterations is unknown.
Condition may be expression or non-zero value.
It is used for complex initialization.
while ( condition )
{ statement(s); }

Solution 6

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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply