Instead of writing long lines for if condition we can short it into a single line. Here we are using ternary operator as a short-hand method of if-else condition in PHP. Therefore, the ternary operator has three operands. We write the condition at the beginning, which is the first operand. Then the symbol â€˜?â€™ separates the condition with the expression to be executed if the condition is true. Finally, the â€˜ : â€˜ symbol separates the true and the false expression. The syntax is written below.
condition ? trueExpression : falseExpression
$text =â€abcâ€; $word = ($text == 'bc') ? "true" : "false"; echo $word;