We now have a youtube channel. Subscribe!

PHP Ternary Operators (Examples)

PHP Ternary Operator


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we will be studying about the PHP Ternary Operator.

Example

Try the following example to have a proper understanding of the ternary operator. Copy and paste the following program in text.php file and keep the file in your PHP Server's document root and then browse it by using any browser.

<html>
   
   <head>
      <title>Ternary Operator</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>

Output

When the above code is executed, it will produce the following result -

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


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about PHP Decision Making.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2022 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain