PHP - Conditional Operator Example


Advertisements


Try following example to understand the conditional operator. Copy and paste following PHP program in test.php file and 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 the following result −


php_operator_types.htm

Advertisements