25

Possible Duplicate: What is “:” in PHP?

What does the : mean in the following PHP code?

<?php
    while (have_posts()) : the_post();
?>
Community
  • 1
  • 1
Paulo Coghi
  • 13,724
  • 14
  • 68
  • 90
  • this is the alternate syntax of some language construct like if while foreach – Shakti Singh Jan 20 '11 at 13:39
  • 3
    Hard to find, but some explanations here: [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – mario Jan 20 '11 at 13:45

7 Answers7

40

It's called an Alternative Syntax For Control Structures. You should have an endwhile; somewhere after that. Basically, it allows you to omit braces {} from a while to make it look "prettier"...

As far as your edit, it's called the Ternary Operator (it's the third section). Basically it's an assignment shorthand.

$foo = $first ? $second : $third;

is the same as saying (Just shorter):

if ($first) {
    $foo = $second;
} else {
    $foo = $third;
}
ircmaxell
  • 163,128
  • 34
  • 264
  • 314
  • 3
    @Shikiryu: I agree completely. I personally don't care for it (I'd rather have the braces), but it's perfectly valid and acceptable to use (and lots of people prefer it). – ircmaxell Jan 20 '11 at 13:54
  • I removed the Ternary Operator part, because it's not this case, but your answer is excellent and helped me understand better. – Paulo Coghi Jan 20 '11 at 13:58
14

There is an example listed in the documentation for while that explains the syntax:

Like with the if statement, you can group multiple statements within the same while loop by surrounding a group of statements with curly braces, or by using the alternate syntax:

while (expr):
    statement
    ...
endwhile;

An answer over here explains it like this:

This (:) operator mostly used in embedded coding of php and html.

Using this operator you can avoid use of curly brace. This operator reduce complexity in embedded coding. You can use this(:) operator with if, while, for, foreach and more...

Without (:) operator

<body>
<?php if(true){ ?>
<span>This is just test</span>
<?php } ?>
</body>

With (:) operator

<body>
<?php if(true): ?>
<span>This is just test</span>
<?php endif; ?>
</body>
aioobe
  • 413,195
  • 112
  • 811
  • 826
8

it's like:

<?php
while(have_posts()) {
    the_post();
}
?>
Víctor B.
  • 1,630
  • 2
  • 14
  • 21
5

This notation is to avoid the use of curly braces - generally when embedding PHP within HTML - and is equivalent to:

while (have_posts())
{
    the_post();
}
Gazler
  • 83,029
  • 18
  • 279
  • 245
3

It's saying while have_posts() is true run the_post().

Matt Lowden
  • 2,586
  • 17
  • 19
2
while (expression is true : code is executed if expression is true)
Jón Trausti Arason
  • 4,548
  • 1
  • 39
  • 46
-2
while(expression = true) : run some code ;
moinudin
  • 134,091
  • 45
  • 190
  • 216
The_Butcher
  • 2,440
  • 2
  • 27
  • 38
  • 1
    Huh? State the obvious much? – ircmaxell Jan 20 '11 at 13:45
  • 1
    obvious questions = obvious answers? – The_Butcher Jan 20 '11 at 13:56
  • 2
    But your code is wrong, since the colon does not indicate the start of a single line loop, it's the start of alternate syntax, so you'd need a `endwhile;` somewhere to terminate the loop. So you just restated the exact code the OP posted with different names, and expect that to be an explanation of what's going on? – ircmaxell Jan 20 '11 at 14:07
  • it makes sense to me :) Well that's not saying much... :/ – The_Butcher Jan 20 '11 at 14:10