Welcome!

Charlie Arehart

Subscribe to Charlie Arehart: eMailAlertsEmail Alerts
Get Charlie Arehart via: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Article

Be Careful in Multi-conditioned CFIF's

Be Careful in Multi-conditioned CFIF's

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

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.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.