Skip to content
On this page

Expressions: Ternary Operations

Ternary Operator

The ternary operator can be used to conditionally evaluate an expression using the ? operator, followed by the expression to evaluate if the condition is true, followed by the : operator, followed by the expression to evaluate if the condition is false.

$foo = $bar ? $bar : $baz;

Short Ternary Operator

The short ternary operator can be used to conditionally evaluate an expression using the ?: operator, followed by the expression to evaluate if the condition is false.

$foo = $bar ?: $baz;

Licensed under either of the MIT License or the Apache License (Version 2.0), at your option.