The expressions string_expression1 and string_expression2 must be string expressions, so that they are not evaluated immediately as the parameters of IIf. For example:
IIf(y is 0, DE("Error"), x/y)
|
If y = 0, this generates an error, because the third expression is the value of x/0 (invalid expression).ColdFusion evaluates string_expression1 and string_expression2. To return the string itself, use the DE function.
Note: If you use number signs (#) in string_expression1 or string_expression2, ColdFusion evaluates the part of the expression in number signs first. If you misuse the number signs, you can cause unexpected results from the IIf function. For example, if you use number signs around the whole expression in string_expression1, and if there is an undefined variable in string_expression1, the function might fail, with the error "Error Resolving Parameter."
|
If a variable is undefined, ColdFusion throws an error when it processes this function. The following example shows this problem:
#IIf(IsDefined("Form.Deliver"), DE(Form.Deliver), DE("no"))#
|
This returns "Error resolving parameter FORM.DELIVER".To avoid this problem, use the DE and Evaluate functions in code such as the following:
#IIf(IsDefined("Form.Deliver"), Evaluate(DE("Form.Deliver")), DE("no"))#
|
This returns "no"; ColdFusion does not throw an error.In the following example, LocalVar is undefined; however, if you omit number signs around LocalVar, the code works properly: