How To Use Ternary Operator Instead Of if else Condition in php

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.

Syntax :

condition ? trueExpression : falseExpression

Example :

$text =”abc”;
$word = ($text == 'bc') ? "true" : "false";
echo $word;