If elseif else or ternary operator to compare numbers PHP?


You can use any of them, but a more professional approach is using the ternary operator (?). Performance of both if-else and ternary operator is the same.

Let’s say we have the following two variable values −

$value1=10;
$value2=0;

We need to compare the above 2 variable values.

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
$value1=10;
$value2=0;
$data=550000;
$output=$value1 ? $value2 : $value2 ? : $data;
echo "The value is=",$output;
?>
</body>
</html>

This will produce the following output

Output

The value is=550000

Updated on: 12-Oct-2020

311 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements