Boolean values can be a time trimmer when you're crunching 10 hours of code in a 5 hour deadline. IF statements can become quite burdensome at times, constantly typing out the conditional. With boolean values, you can shortcut that conditional typing slightly. Here is why.

    <cfset flag = 1 />
    <cfset results = "" />
    
    <cfif flag eq 1>
        <cfset results = "foo" />
    <cfelse>
        <cfset results = "bar" />
    </cfif>

Pretty simple, right? No hidden functionality here. Let's take a "little off the top", barbershop-style, showing you how I do it.

Voila! Overall, it's not much, but with IF statements in bulk this can be a helluva little trick.

How does it work? Well, the value of flag is set to "1". In boolean terms, 1 means true/on, while 0 means false/off (Access DB users will notice this as well). Since an IF statement, at its base, merely checks one of two possible conditions for a variable, this works perfectly.

<cfif flag> literally processes to <cfif 1>, and since 1 means "true", that part of the IF statement is executed.

Coincidentally, this technique can be done if your variable is has a value of "TRUE" or "FALSE", as ColdFusion treats these two words in the same manner as 1/0. Now, back to your regularly scheduled program.

About This Tutorial
Author: David Patricola
Skill Level: Intermediate 
 
 
 
Platforms Tested: CF4,CF5,CFMX
Total Views: 104,969
Submission Date: July 24, 2003
Last Update Date: August 25, 2011
All Tutorials By This Autor: 1
Discuss This Tutorial
Advertisement

Sponsored By...
Mobile App Development (IOS, Android, Cordova, Phonegap, Objective-C, Java) - Austin, Texas Mobile Apps - Touch512, LLC.