Use Ternary Operator in PHP as 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;

Example

The PHP code is as follows

 Live Demo

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

Output

This will produce the following output

Greater Than or Equal to 100
Updated on: 2020-10-13T06:56:46+05:30

336 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements