Conditional Operators In PHP

 From The Below Example you will get to know about conditional operators


<?php

$num1 = 10;

$num2 = 5;

// Ternary operator

$result = ($num1 > $num2) ? "Num1 is greater" : "Num2 is greater";

echo $result . "\n"; //

 Output: Num1 is greater

?>

In this example, we first declare two variables $num1 and $num2 and assign them values. Then we use the ternary operator ? : to evaluate the condition ($num1 > $num2) and assign the result to the $result variable. If the condition is true, the value on the left side of the colon : is assigned, otherwise the value on the right side is assigned. Finally, we use the echo statement to print the result to the screen.


Comments