What does ":" mean in PHP?

Possible Duplicate: What is “:” in PHP?

What does the : mean in the following PHP code?

<?php
    while (have_posts()) : the_post();
?>

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;
}

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>

就像是:

<?php
while(have_posts()) {
    the_post();
}
?>
链接地址: http://www.djcxy.com/p/1744.html

上一篇: PHP中的两个冒号意味着什么?

下一篇: “:”在PHP中意味着什么?