Home/Tutorial/PHP/What does “:” mean in PHP?
What does “:” mean in PHP? 2016-12-17T04:27:54+00:00

What does the : mean in the following PHP code?


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”. Basically it’s an assignment shorthand.

is the same as saying (Just shorter):


What does it mean by “?”

This is the PHP ternary operator (also known as a conditional operator) – if first operand evaluates true, evaluate as second operand, else evaluate as third operand.

$param = isset($_GET[‘param’]) ? $_GET[‘param’] : ‘default’;
There’s also a shorthand version of this (in PHP 5.3 onwards). You can leave out the middle operand. The operator will evaluate as the first operand if it true, and the third operand otherwise. For example:

$result = $x ?: ‘default’;

It is worth mentioning that the above code when using i.e. $_GET or $_POST variable will throw undefined index notice and to prevent that we need to use a longer version, with isset or a null coalescing operator which is introduced in PHP7:

$param = $_GET[‘param’] ?? ‘default’;
$foo = $first ? $second : $third;

if ($first) {
$foo = $second;
} else {
$foo = $third;