How do I use the ternary operator ( ? : ) in PHP as a shorthand for “if / else”?

The syntax is as follows −

$anyVariableName=yourCondition ? if condition becomes true then this will be executed: if condition becomes false then this gets executed;


The PHP code is as follows

 Live Demo

<!DOCTYPE html>
   $result=$value >=100 ? "Greater Than or Equal to 100":"Less Than 100";
   echo $result;


This will produce the following output

Greater Than or Equal to 100