Creates test conditional statements using an expression, a variable or a function to return a value. It can be optionally used with both the cfelse and cfelseif tags.
Other
<cfif [contition="Boolean"]/>
[] = Optional attribute
| Name | Type | Required | Default | Description |
| contition | Boolean | No | Contition o the expression |
Example:
In this example we will show how to vary the display results of a database query, which can be manipulated by using a very simple comparison expression in the CFIF tag.
<--- Fetch the list of countries ---> <cfquery name="getCountries" dbsource="regionalData"> SELECT country_ID, country_Name, country_Capital FROM countries ORDER BY country_Name </cfquery> <--- Display our results using different conditions ---> <p>First we list all the countries in the database.</p> <ul id="listCntry"> <cfquery query="getCountries"> <li>#getCountries.country_Name#</li> </cfquery> </ul> <p>Now we will display only the countries that start with the letter A using a conditional CFIF expression statement.</p> <ul id="listCntryA"> <cfquery query="getCountries"> <cfif Left(getCountries.country_Name, 1) IS 'A'> <li>#getCountries.country_Name#</li> </cfif> </cfquery> </ul> <p>By introducing the CFELSEIF tag we can introduce multiple conditions.</p> <ul id="listCntryAUZ"> <cfquery query="getCountries"> <cfif Left(getCountries.country_Name, 1) IS 'A'> <li>#getCountries.country_Name#</li> <cfelseif Left(getCountries.country_Name, 1) IS 'U'> <li>#getCountries.country_Name#</li> <cfelseif Left(getCountries.country_Name, 1) IS 'Z'> <li>#getCountries.country_Name#</li> </cfif> </cfquery> </ul> <p>We can also merge these multiple conditions into a single statement.</p> <ul id="listCntryAUZ"> <cfquery query="getCountries"> <cfif Left(getCountries.country_Name, 1) IS 'A' OR Left(getCountries.country_Name, 1) IS 'U' OR Left(getCountries.country_Name, 1) IS 'Z'> <li>#getCountries.country_Name#</li> </cfif> </cfquery> </ul> <p>Using the CFELSE tag we can list all the countries and empathize the ones which start with A or Z and strongly emphasize the countries that start with U.</p> <ul id="listedCountries"> <cfquery query="getCountries"> <cfif Left(getCountries.country_Name, 1) IS 'A' OR Left(getCountries.country_Name, 1) IS 'Z'> <li><em>#getCountries.country_Name#</em></li> <cfelseif Left(getCountries.country_Name, 1) IS 'U'> <li><strong>#getCountries.country_Name#</strong></li> <cfelse> <li>#getCountries.country_Name#</li> </cfif> </cfquery> </ul>