CFIF CFELSEIF CFELSE

Used with CFELSE and CFELSEIF, CFIF lets you create simple and compound conditional statements in CFML. The value in the CFIF tag can be any expression.

Syntax

<CFIF expression>
    HTML and CFML tags
<CFELSEIF>
    HTML and CFML tags
<CFELSE expression>
    HTML and CFML tags
</CFIF>

Usage

Note that when testing for the return value of any function that returns a Boolean, you do not need to explicitly define the TRUE condition. The following code uses IsArray as an example:

<CFIF IsArray(myarray)>

When successful, IsArray evaluates to YES, the string equivalent of the Boolean TRUE. This method is preferred over explicitly defining the TRUE condition:

<CFIF IsArray(myarray) IS TRUE>

In addition to using the decision functions in the expression, such as IsArray and IsBoolean, you can also use any of the ColdFusion operators in the expression.

CFML Operators 

CFML Operator

Alternate

Checks that the right value is

IS EQUAL, EQ equal to the left value.
IS NOT NOT EQUAL, NEQ is not equal to the left value.
CONTAINS is contained within the left value.
DOES NOT CONTAIN is not contained within the left value.
GREATER THAN GT is greater than the left value.
LESS THAN LT is less than the left value.
GREATER THAN OR EQUAL GTE is greater than or equal to the left value.
LESS THAN OR EQUAL LTE is less than or equal to the left value.

Note

Do not substitute an equal sign (=) for the IS operator.

Example

<!--- This example shows the interaction of CFIF, CFELSE,
and CFELSEIF --->
...
<H3>CFIF Example</H3>

<P>CFIF gives us the ability to perform conditional logic
based on a condition or set of conditions.
<P>For example, we can output the list of Allaire departments from the
HRAPP datasource and only display them <B>IF</B>
the location = Cambridge.

<CFQUERY name="getAllaireDepartments" datasource="HRAPP">
SELECT Department_Name, Location
From Departments
</cfquery>
<hr>
<!--- use CFIF to test a condition when outputting a query --->
<P>The following departments are in Cambridge:

<CFOUTPUT QUERY="getAllaireDepartments" >
<CFIF location is "Cambridge">
    <BR><Department:</B>#Department_Name#
</CFIF>
</CFOUTPUT>

<CFQUERY name="getAllaireEmployees" datasource="HRAPP">
SELECT Department_Name, Location
From Departments
</cfquery>

<P>If you would like more than one condition to be the case,
you can ask for a list of departments in Cambridge <B>OR</B>
San Francisco.  If the department does not meet either condition, you
can use CFELSE to show only the names and cities of the
other departments.

<CFOUTPUT QUERY="getAllaireEmployees">
<CFIF location is "San Francisco" OR location is "Cambridge">
    <BR><I>#Department_Name#, #Location#</I>
<CFELSE>
    <BR><I>#Department_Name#, #Location#</I>
</CFIF>
</CFOUTPUT>    
    

<!--- use CFIF to specify a conditional choice for multiple
options; also note the nested CFIF --->
<hr>
<P>You can also use CFELSEIF to provide one or more conditions.</P>

<CFOUTPUT QUERY="getAllaireDepartments">
<CFIF location is "San Francisco">
    <BR><I>#Department_Name#, #Location#</I>
<CFELSEIF location is "Cambridge">
    <BR><I>#Department_Name#, #Location#</I>
<CFELSEIF location is "London">
    <BR><I>#Department_Name#, #Location#</I>
<CFELSE>
    <BR><I>#Department_Name#, #Location#</I>
</CFIF>
</CFOUTPUT>    

<P>For a more complex example of the use of CFIF, see <A 
HREF="listcontains.cfm">ListContains Example</A>.</P>
</BODY>
</HTML>