| By Charlie Arehart | Article Rating: |
|
| September 17, 2003 12:00 AM EDT | Reads: |
8,100 |
When performing a CFIF testing multiple conditions, be careful when testing against a value that could equate to a Boolean true or false (0 or 1, yes or no, etc.). For instance, if you wanted to test whether a form variable called "answer" was either 1 or 0, how would you code the IF test? If you tested the condition:
CFIF FORM.ANSWER IS 0 OR 1
the CF interpreter would not complain. Technically, that's a valid condition. The problem is that it will always be true, regardless of the value of FORM.ANSWER. This is because the interpreter is really being asked to consider whether "FORM.ANSWER IS 0" is true or "1" is true. That's almost certainly not what's intended by someone writing this condition, but since the string "1" alone always equates to "true", the condition (since it's an OR test) is always true.
The correct form of this condition should be:
CFIF FORM.ANSWER IS 0 or FORM.ANSWER IS 1
The good news is that this possibility of misinterpretation won't happen with just any condition. If the value left on its own in the first condition above (as in the "1" in that example) was not a value that could equate to a Boolean true or false, the interpreter would instead reject the statement as invalid. That is:
CFIF FORM.STATE IS "MA" OR "VT"
would cause a compilation error "Cannot convert x to Boolean" because "VT" is not a valid Boolean. Now that you understand how a Boolean could be (mis)used, perhaps the message in this error would make more sense. The challenge is simply in remembering that you must not test multiple values in a condition in the manner done above. You must repeat each condition fully.
If you have more than a couple of values against which to test, though, the syntax can become unwieldy. The next tip offers a solution to that.
Charlie Arehart Education Director, Fig Leaf Software
Published September 17, 2003 Reads 8,100
Copyright © 2003 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Charlie Arehart
A veteran ColdFusion developer since 1997, Charlie Arehart is a long-time contributor to the community and a recognized Adobe Community Expert. He's a certified Advanced CF Developer and Instructor for CF 4/5/6/7 and served as tech editor of CFDJ until 2003. Now an independent contractor (carehart.org) living in Alpharetta, GA, Charlie provides high-level troubleshooting/tuning assistance and training/mentoring for CF teams. He helps run the Online ColdFusion Meetup (coldfusionmeetup.com, an online CF user group), is a contributor to the CF8 WACK books by Ben Forta, and is frequently invited to speak at developer conferences and user groups worldwide.
- Validating Input with Regular Expressions
- Getting Focus()ed and a Quick JavaScript Lesson
- Setting Up Your Development Server with ColdFusion 5, MX, and BlueDragon
- Toward Better Error Handling
- Monitoring Your ColdFusion Environment With the Free Log Parser Toolkit
- New Possibilities for Session/Client Variable Handling in CFMX
- Testing Existence in Arrays
- Making the Case for CFML on J2EE
- Exploring Amazon Web Services with ColdFusion MX
- Getting Focused - and a Quick JavaScript Lesson
- CFML on J2EE: Easy as 1-2-3
- Getting into HomeSite+



























