This is called a ternary expression.
http://php.net/manual/en/language.expressions.php
It should be noted that this is not an “alternative syntax for if”, since it should not be used to control program flow.
In a simple example of setting variables, it can help you avoid lengthy ones if such statements look like this: (ref: php docs )
// Example usage for: Ternary Operator $action = (empty($_POST['action'])) ? 'default' : $_POST['action']; // The above is identical to this if/else statement if (empty($_POST['action'])) { $action = 'default'; } else { $action = $_POST['action']; }
However, it can be used in places other than simply assigning a variable.
function say($a, $b) { echo "{$a} {$b}"; } $foo = false; say('hello', $foo ? 'world' : 'planet');
maček
source share