PHP - Conditional Operator Example


Advertisements


Try following example to understand the conditional operator. Copy and paste following PHP program in test.php file ando keep it in your PHP Server's document root and browse it using any browser.

<html>
<head><title>Arithmetical Operators</title><head>
<body>
<?php
    $a = 10;
    $b = 20;
    
    /* If condition is true then assign a to result otheriwse b */
    $result = ($a > $b ) ? $a :$b;
    echo "TEST1 : Value of result is $result<br/>";
    /* If condition is true then assign a to result otheriwse b */
    $result = ($a < $b ) ? $a :$b;
    echo "TEST2 : Value of result is $result<br/>";
?>
</body>
</html>

This will produce following result

TEST1 : Value of result is 20
TEST2 : Value of result is 10

php_operator_types.htm