Categories
Code Area PHP

What is PHP Ternary Operator

PHP Ternary Operator is the Shorthand of if/else statement. It’s is important to know “What is it, How to use it ” for php programmer.

It’s simply said that the shorthand of if/else statement. It’s rally easy to use and reduce you code.

Ternary Operator using “(condition) ? (return true value) : (return false value)” for execute the statement.It’s called Ternary Operator because it’s use three operands, one condition, one result for true, one result for false. Hear is an example.

<?php
    $age=10;
    $Status= ($age<18)? “Child”:”Adult”;
    echo $Status;
?>

Output

Child

NB:  The most important thing of using it reduce your code ,save the executing time.Makes maintaining code quicker, easier.