What does the symbol :
mean in PHP?

- 30,738
- 21
- 105
- 131

- 21,085
- 65
- 193
- 298
-
9Could you please post the code that it appears in? It could have different meanings, depending on the context. – FrustratedWithFormsDesigner May 25 '10 at 20:05
-
9When asking a questions please `be specific`. Oftentimes, this means giving a code example. – Armstrongest May 25 '10 at 20:16
-
Scope Resolution Operator(:) http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php – Mahendra Jella Nov 23 '13 at 09:49
6 Answers
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.

- 3,656
- 1
- 25
- 25
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.

- 795,719
- 175
- 1,089
- 1,143
-
-
Could you please help me.I'm doubtful about the use of ' : ' in the following context ( Laravel ). ['email' => 'required|email|unique:email,users'] – Michel Oct 27 '17 at 06:59
-
@Michel: I don’t know about Laravel, sorry. From PHP‘s perspective, `:` is part of a string so it has no special meaning. – Felix Kling Oct 27 '17 at 07:34
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.

- 176,543
- 40
- 303
- 368
Shorter if statement:
$val = (condition) ? "condition is true" : "condition is false";

- 30,738
- 21
- 105
- 131
It can mean a number of things. You may mean the ternary operator, ?:
.

- 30,738
- 21
- 105
- 131

- 84,206
- 33
- 197
- 283
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.

- 3,482
- 19
- 35