0

I am having trouble finding out what the purpose of this semicolon is at for (; . My searches online have only brought irrelevant results. Can someone explain this please?

for (; $this->foo <=10; $this->foo++) {}
Dan
  • 951
  • 1
  • 23
  • 46

3 Answers3

3

This basically skips the initializing of the iterator variable. Normally you'd type something like this:

for ($i = 0; $this->foo <= 10; $i++) {}

in your example however your object which is accessed by the $this simply accumulates the foo by 1;

cptnk
  • 2,430
  • 18
  • 29
2

The first expression in a for loop is being executed at the beginnig of the loop, usually for initializing a variable.

In this for loop the expression is empty, so nothing is going to happen at the beginning.

Marce
  • 467
  • 1
  • 6
  • 10
2

For loop can work fine with for(;;) which is known as infinite loop. It doesn't have any initialization part, no conditional checks and no post operation.

for(initialization, condition check, increment/decrement)

These are the 3 parts of for loop

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78