Skip to content

Quick Tip: JavaScript Ternary Operator and PHP Ternary Operator

In computer programming, ?: is a ternary operator that is part of the syntax for basic conditional expressions in several programming languages. It is commonly referred to as the conditional operator, inline if (if), or ternary if. An expression a ? b : c evaluates to b if the value of a is true, and otherwise to c. One can read it aloud as “if a then b otherwise c“.


$result = ( $marks >= 40 ) ? 'pass' : 'fail'; // PHP

ref: ( David Walsh ) https://davidwalsh.name/php-shorthand-if-else-ternary-operators


let result = ( marks >= 40 ) ? 'pass' : 'fail'; // JavaScript

ref: https://www.programiz.com/javascript/ternary-operator