What is Ternary Operator ?
t’s a shortcut for an if-else statement, and is also known as a conditional operator. Its operate on the three input
Ternary operators save time and lines of code. Its more preferable when you are performing simple conditional operation.
In PHP it works as:
“boolean_condition?true_value:false_value”
In coldfusion it works as:
There is not any direct implementation of operator, but coldfusion uses function IIf for the same purpose
Syntax : IIf(condition, string_expression1, string_expression2)
Without Ternary Operator
var a=1,c=”";
if(a==1)
{
c=”Hello”;
}
else
{
c=”Bye”;
}
With Ternary Operator
var a=1,c=”";
c=(a==1)?”Hello”:”Bye”;
November 24, 2006 at 6:55 am |
This really somthing cool. It saves lot of efforts of if – else.
Good.
December 15, 2007 at 1:11 pm |
very interesting, but I don’t agree with you
Idetrorce
February 21, 2008 at 4:57 am |
Thanks! I’m coming from a bunch of languages based on C/C++ (JavaScript, Java, C++, PHP) and I figured ColdFusion had to have some way to shorten an if-else.
Thanks again!
Chris