7

What does the below code mean (it's a lambda in the while-statement, then a colon after)? Coming from JavaScript, I have no idea what that means or even how to search for that. Can anyone help explain this?

while ($query->have_posts()): $query->the_post();

Btw I got this from WordPress but is the syntax pure PHP?

TruMan1
  • 33,665
  • 59
  • 184
  • 335

3 Answers3

9

This is a less-common while structure:

while (condition):
    doSomething();
endwhile;

This is not how things are traditionally done, but it is valid syntax. See while loop syntax for more details, as well as alternative control syntax.

bwoebi
  • 23,637
  • 5
  • 58
  • 79
Levi Morrison
  • 19,116
  • 7
  • 65
  • 85
1

It is an alternative syntax for control structures.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

In general, this:

while (expression) :
  //actions
endwhile;

is the same as this:

while (expression) {
  //actions
}

which is what you're probably used to. The bracketted expression $query->havePosts() is just the condition of the while, and the sentence after the colon is the first line within the while.

aaaaaa123456789
  • 5,541
  • 1
  • 20
  • 33