Name

CFSWITCH

Synopsis

<CFSWITCH EXPRESSION="expression">
  <CFCASE VALUE="value" DELIMITERS="delimiter">
  HTML and CFML
  </CFCASE>
  <CFCASE VALUE="value" DELIMITERS="delimiter">
  HTML and CFML
  </CFCASE>
  ...
  <CFDEFAULTCASE>
  HTML and CFML
  </CFDEFAULTCASE>
</CFSWITCH>

Evaluates an expression against multiple values until a match is made. Once a match has been made, CFSWITCH passes control to the appropriate CFCASE tag where additional action can take place. If no VALUE matches EXPRESSION, the code between the CFDEFAULTCASE tags is executed.

CFSWITCH/CFCASE/CFDEFAULTCASE is similar in function to CFIF/CFELSEIF/CFELSE. CFSWITCH is significantly faster than using multiple CFIF statements however, because the CFCASE statements are evaluated when the ColdFusion language parser compiles the template into p-code. This means that there is no difference in execution time between a CFSWITCH statement with one CFCASE statement or 1,000 CFCASE statements. CFIF statements on the other hand are evaluated one by one at execution time until one statement evaluates True.

Attributes

EXPRESSION=" expression "

Any ColdFusion expression that evaluates to a simple value (string, number, Boolean, date/time). Required.

VALUE=" value "

A value or delimited list of values CFSWITCH uses to perform a case-insensitive comparison against the specified EXPRESSION. If a value listed in VALUE matches EXPRESSION, the code between the CFCASE tags is executed. Required.

DELIMITERS=" delimiter "

Specifies the character ...

Get Programming ColdFusion now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.