开发者

What is ":" in PHP? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
开发者_StackOverflow中文版

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

Improve this question

What does the symbol : mean in PHP?


PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.


You also encounter : if you use the alternative syntax for control structures:

<?php
if ($a == 5):
    echo "a equals 5";
    echo "...";
elseif ($a == 6):
    echo "a equals 6";
    echo "!!!";
else:
    echo "a is neither 5 nor 6";
endif;
?>

Or as already mentioned the ternary operator:

$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

(Examples taken from the documentation)


Edit: Somehow I didn't see that the alternative syntax was already mentioned, must be too tired ;) Anyway, I will leave it as it is, as I think an actual example and a link to the documentation is more helpful than just plain text.


I'm guessing you're seeing this syntax:

print ($item ? $item : '');

This is a short form of if/else. The ? is the if, and the : is the else.


Shorter if statement:

$val = (condition) ? "condition is true" : "condition is false";


As others have posted, you probably are looking at ternary logic.

However, if two of them are together, then it is the scope resolution operator, used for referencing status methods/properties and constants.


It can mean a number of things. You may mean the ternary operator, ?:.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜