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
  • 871
  • 1
  • 21
  • 40

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,420
  • 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
  • 480
  • 1
  • 7
  • 9
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
  • 11,641
  • 10
  • 42
  • 69