IIF and DE Explained

 
Dec 13, 1999

by Michael Dinowitz

This is an excerpt from an upcoming book of Michael Dinowitz's, ColdFusion Functions, being published by JM Publishers, Inc.

IIF Explained

This is based on data from my tests and from my forthcoming book, ColdFusion Functions. IIF is an extremely powerful function that is not used as often as it should because people rarely know what it does or how to use it effectively. I'm going to change that here and now. Oh, we'll also discuss the DE function, as it's really only used within an IIF function.

IIF

IIF simulates an IF statement for a single operation. It says, try a comparison statement (a statement that has one or more clauses and will evaluate to either TRUE or FALSE). If the result is TRUE, Evaluate the value in position 2 of the function. If the result is FALSE, Evaluate the value in position 3 of the function.

Now the comparison statement is not what we'll be covering at this time (it will be covered in Core ColdFusion chap. 6). Instead, we'll be covering the evaluation of the values in positions 2 and 3 of the function. (Let me warn you that this is where almost everyone gets confused.)

Below is a script that runs 4 different versions of some IIF code. We're only going to look at the second position. You'll note that the third position is 1. Being that numbers cannot be variables, ColdFusion sees the number and does not try to evaluate it.

<CFSCRIPT>
   Michael="yoda";
   Name="Michael";
   
   //double evaluation. Evaluates name to michael and then evaluates michael to 'yoda'
   WriteOutput(IIF(1 is 1, name, 1)&'<br>');
   
   //single evaluation. Evaluates name to 'michael'
   WriteOutput(IIF(1 is 1, 'name', 1)&'<br>');	
   
   //single evaluation. Evaluates name to 'michael'	
   WriteOutput(IIF(1 is 1, de(name), 1)&'<br>');	
   
   //no evaluation. prints out 'name'
   WriteOutput(IIF(1 is 1, de('name'), 1)&'<br>');	
</CFSCRIPT>

In our first case, we have a single variable in position 2. When it's run, this variable will be evaluated and its value (which is also a variable) will be evaluated, resulting in a double evaluation. This is because of a combination of tag syntax and usage. In a normal function, anything that's not within quotes is considered a variable (unless it's a number.) (See appendix on Evaluation Zones in the upcoming ColdFusion Functions book). Therefore, name is evaluated as a basic part of the function before the IIF statement even kicks in. Now the IIF function says that it will evaluate whatever's in positions 2 and 3. Therefore, the value of name (Michael) is evaluated.

In the second example, we escape this by placing name inside quotes. (As a side note, I only use single quotes inside of functions. Double quotes are for tag attributes only. See the ColdFusion Functions book's appendix on formatting.) This avoids the first evaluation, due to standard function syntax. The second evaluation still takes place, as this is part of the purpose of the function.

DE and IIF

Our third example has the same result as the second arrives at it by a different means. The DE (Delay Evaluation) function tells IIF not to evaluate its contents. This happens because the result of a DE function is its input value wrapped in double quotes with any internal double quotes escaped.
<CFSCRIPT>
   Name="Michael";
   Posessive="Michael's";
   	
   //The word Name in a DE function will result in "Name"
   WriteOutput(de('Name')&'<br>');
   	
   //Evaluation of Name. The result is "Michael"
   WriteOutput(de(Name)&'<br>');
   		
   //Evaluation of Possessive. Quotes inside the result are escaped as well as wrapped to give "Michael's"
   WriteOutput(de(Posessive)&'<br>');		
</CFSCRIPT>

But its contents are a variable, which any function will evaluate. It's not until we get to the fourth IIF example that we have a total lack of evaluation. In that one, the DE function contains NO variables, only text. The DE function says that this text should not be evaluated and IIF returns the plain text with no evaluation. Four different examples. Three different results. Two Rarely used functions. One informative document.


IIF

(Condition, String_Expression1, String_Expression2)

Condition

(Required; accepts: Any) Any expression that can be evaluated as a Boolean.

String_Expression1

(Required; accepts: Any) Valid string expression to be evaluated and returned if condition is TRUE.

String_Expression2

(Required; accepts: Any) Valid string expression to be evaluated and returned if condition is False.
Return Data:
Category:
Related Functions:

IIF simulates an IF statement for a single operation. It says, try a comparison statement (a statement that has one or more clauses and will evaluate to either TRUE or FALSE). If the result is TRUE, Evaluate the value in position 2 of the function. If the result is FALSE, Evaluate the value in position 3 of the function.

<CFSCRIPT>
   Michael="yoda";
   Name="Michael";
   
   //double evaluation. Evaluates name to michael and then evaluates michael 
   // to 'yoda'
   WriteOutput(IIF(1 is 1, name, 1)&'<br>');
   
   //single evaluation. Evaluates name to 'michael'
   WriteOutput(IIF(1 is 1, 'name', 1)&'<br>');
   	
   //single evaluation. Evaluates name to 'michael'	
   WriteOutput(IIF(1 is 1, de(name), 1)&'<br>');
   	
   //no evaluation. prints out 'name'
   WriteOutput(IIF(1 is 1, de('name'), 1)&'<br>');	
</CFSCRIPT>

String
Dynamic Evaluation
DE, Evaluate,

DE

(string)

string

(Required; accepts: String) String to be evaluated with delay.
Return Data:
Category:
Related Functions:

The result of a DE function is its input value wrapped in double quotes with any internal double quotes escaped. The DE (Delay Evaluation) function prevents the evaluation of a string as an expression when it is passed as an argument to IIf or Evaluate.

<CFSCRIPT>
   Name="Michael";
   Posessive="Michael's";
   	
   //The word Name in a DE function will result in "Name"
   WriteOutput(de('Name')&'<br>');
   	
   //Evaluation of Name. The result is "Michael"
   WriteOutput(de(Name)&'<br>');
   		
   //Evaluation of Name. Quotes inside the result are escaped as well 
   // as wrapped to give "Michael's"
   WriteOutput(de(Posessive)&'<br>');		
</CFSCRIPT>

String
Dynamic Evaluation
Evaluate

Add a Comment
(If you subscribe, any new posts to this thread will be sent to your email address.)
  
Privacy | FAQ | Site Map | About | Guidelines | Contact | Advertising | What is ColdFusion?
House of Fusion | ColdFusion Jobs | Blog of Fusion | AHP Hosting