IIf

The function evaluates its condition as a Boolean. If the result is TRUE, it returns the value of Evaluate(string_expression1); otherwise, it returns the value of Evaluate(string_expression2).

Prior to using IIf, please read the Usage section and Note carefully. The IIf function is primarily intended for the conditional processing of dynamic expressions.

For general conditional processing, see CFIF CFELSEIF CFELSE.

For error handling, see CFTRY CFCATCH.

See also DE and Evaluate.

Syntax

IIf(condition, string_expression1, string_expression2)
condition

Any expression that can be evaluated as a Boolean.

string_expression1

Valid string expression to be evaluated and returned if condition is TRUE.

string_expression2

Valid string expression to be evaluated and returned if condition is FALSE.

Usage

The IIf function is a shortcut for the following construct:

<CFIF condition>
    <CFSET result=Evaluate(string_expression1)>
<CFELSE>
    <CFSET result=Evaluate(string_expression2)>
</CFIF>

returning result.

The expressions string_expression1 and string_expression2 must be string expressions, so that they do not get evaluated immediately as the arguments of IIf. For example:

IIf(y is 0, DE("Error"), x/y)

will generate error if y=0 because the third argument is the value of x/0 (not a valid expression).

Remember that ColdFusion evaluates string_expression1 and string_expression2. To return the string itself instead of evaluate the expression, use the DE (delay evaluation) function.

Note If you use pound signs (#) in either string_expression1 or string_expression2, ColdFusion evaluates the part of the expression that is in pound signs first. By misusing pound signs, you can skew the results of the IIf function. In particular, if you use pound signs around the whole expression in string_expression1, it can cause the function to fail with the error 'Error Resolving Parameter' if there is an undefined variable in string_expression1.

For example, "LocalVar" is undefined, however, the following logic functions as you would expect if you do not use pound signs around "LocalVal":

<CFOUTPUT>
#IIf(IsDefined("LocalVar"), "LocalVar", DE("The variable is not 
defined."))#
</CFOUTPUT>

The output is:

The variable is not defined.

Whereas, the pound signs around "LocalVar" in the following code cause it to fail with the error message 'Error Resolving Parameter', because ColdFusion never has a chance to evaluate the original condition IsDefined("LocalVar").

<CFOUTPUT>
#IIf(IsDefined("LocalVar"), DE("#LocalVar#"), DE("The variable is not 
defined."))#
</CFOUTPUT>

The error message would be:

Error resolving parameter LOCALVAR

The DE function has no impact on the evaluation of LocalVal, since the pound signs cause it to be evaluated immediately.

<!--- This example shows IIf --->
<HTML>
<HEAD>
<TITLE>
IIf Example
</TITLE>
</HEAD>

<BODY bgcolor=silver>
<H3>IIf Function</H3>

<P>IIf evaluates a condition, then  performs an Evaluate on 
string expression 1 or string expression 2 depending on the 
Boolean outcome <I>(TRUE = run expression 1; FALSE = run
expression 2)</I>.
</P>

<P>The result of the expression 
IIf( Hour(Now()) GT 12, 
  DE("It is afternoon or evening"), 
    DE("It is morning"))
is:<BR>
<CFOUTPUT>
#IIf( Hour(Now()) GT 12, 
  DE("It is afternoon or evening"), 
    DE("It is morning"))#
</CFOUTPUT>
</P>

</BODY>
</HTML>       



1