Ternary Operator implemenation in PHP and Coldfusion

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”;

Advertisements

3 Responses to Ternary Operator implemenation in PHP and Coldfusion

  1. purva says:

    This really somthing cool. It saves lot of efforts of if – else.

    Good.

  2. Idetrorce says:

    very interesting, but I don’t agree with you
    Idetrorce

  3. Chris says:

    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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: