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.
<cfif expression> HTML and CFML tags <cfelseif> HTML and CFML tags <cfelse expression> HTML and CFML tags </cfif>
cfabort,
cfbreak,
cfexecute,
cfexit,
cflocation,
cfloop,
cfswitch cfcase cfdefaultcase,
cfthrow,
cftry cfcatch
When testing the return value of a function that returns a Boolean, you do not have 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>
<cfif "11/23/1998 " GT "11/15/1998 ">
This switch is set on the ColdFusion Administrator Server Settings page. For more information, see Advanced ColdFusion Administration.
<!--- This example shows the interaction of cfif, cfelse, and cfelseif ---> ... <H3>cfif Example</H3> <P>cfif lets you perform conditional logic based on a condition or set of conditions. <P>For example, you can output the list of Centers from the snippets datasource by group and only display <B>IF</B> the city = San Diego. <hr> <!--- use cfif to test a condition when outputting a query ---> <P>The following are centers in San Diego: <cfoutput query = "getCenters" > <cfif city is "San Diego"> <BR><B>Name/Address:</B>#Name#, #Address1#, #City#, #State# <BR><B>Contact:</B> #Contact#<BR> </cfif> </cfoutput> <P>If you want more than one condition to be the case, ask for a list of the centers in San Diego <B>OR</B> Santa Ana. If the center does not follow this condition, use cfelse to show only the names and cities of the other centers. <P>Notice how a nested cfif is used to specify the location of the featured site (Santa Ana or San Diego). <!--- use cfif to specify a conditional choice for multiple options; also note the nested cfif ---> <hr> <P>Complete information is shown for centers in San Diego or Santa Ana. All other centers are listed in italics: <cfoutput query = "getCenters"> <cfif city is "San Diego" OR city is "Santa Ana"> <H4>Featured Center in <cfif city is "San Diego">San Diego<cfelse>Santa Ana</cfif></H4> <B>Name/Address:</B>#Name#, #Address1#, #City#, #State# <BR><B>Contact:</B> #Contact#<BR> <cfelse> <BR><I>#Name#, #City#</I> </cfif> </cfoutput> <P>Finally, use cfelseif to cycle through a number of conditions and produce varying output. Note that you can use cfcase and cfswitch for a more elegant representation of this behavior. <hr> <P> <!--- use cfif in conjunction with cfelseif to specify more than one branch in a conditional situation ---> <cfoutput query = "getCenters"> <cfif city is "San Diego" OR city is "Santa Ana"> <BR><I>#Name#, #City#</I> (this one is in <cfif city is "San Diego">San Diego<cfelse>Santa Ana</cfif>) <cfelseif city is "San Francisco"> <BR><I>#Name#, #City#</I> (this one is in San Francisco) <cfelseif city is "Suisun"> <BR><I>#Name#, #City#</I> (this one is in Suisun) <cfelse> <BR><I>#Name#</I> <B>Not in a city we track</B> </cfif> </cfoutput> </body> </html>