Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ColdFusion "Yes" eq "True"; "No" eq "False

Tags:

coldfusion

Why is it ?

<cfif "yes" eq "true">

Yes equals true.

<cfelse>

Yes does not equal true.

</cfif>

The page outputs. "Yes equals true."

like image 553
rique Avatar asked Mar 19 '13 08:03

rique


People also ask

Is yes equal to true in ColdFusion?

In coldfusion, "yes" and "true" are interpreted as bit(1) likewise "no" and "false" are interpreted as bit(0),so "yes" and "true" are equal. in same way <cfif 1 eq "true"> Yes equals true.

Should I use ColdFusion to check for true and false variables?

Since FALSE is always 0 (or NO or FALSE), it's usually better to check for NOT FALSE or NEQ 0 than it is to check for TRUE. However, even with that, you still have watch out. Determine what you want to consider TRUE and do a full evaluation for that. ColdFusion sometimes casts TRUE and FALSE variables differently.

Is the value of-1 always false?

And -1 is still TRUE (special thanks MS Access Yes/No datatypes :-p). Since FALSE is always 0 (or NO or FALSE), it's usually better to check for NOT FALSE or NEQ 0 than it is to check for TRUE.

How to compare strings in ColdFusion?

10 ColdFusion has dynamic variable types, and does a few odd conversions at runtime, strings can be used as booleans, dates and numbers without casting them which has both advantages and disadvantages. if you need to do a simple string comparison you can use the built in compare() function.


3 Answers

Chris' and Keshav's answers are correct, ColdFusion converts the values. Here is the official documentation on ColdFusion data conversions.

Conversion between types

Value     As Boolean     As number     As date-time     As string

"Yes"     True           1             Error            "Yes"

"No"      False          0             Error            "No"

True      True           1             Error            "Yes"

False     False          0             Error            "No"
like image 139
genericHCU Avatar answered Nov 18 '22 17:11

genericHCU


ColdFusion has dynamic variable types, and does a few odd conversions at runtime, strings can be used as booleans, dates and numbers without casting them which has both advantages and disadvantages.

if you need to do a simple string comparison you can use the built in compare() function.

Ben Nadel gives a good summary of string comparison options here - http://www.bennadel.com/blog/236-ColdFusion-String-Comparison-Compare-vs-Equals-vs-CompareTo-.htm

like image 45
Chris Blackwell Avatar answered Nov 18 '22 16:11

Chris Blackwell


You have to be very careful with the true/false comparisons. They should be simple, but sometimes, depending on how you write your code, the evaluations of TRUE/FALSE may be different. By pretty much every definition, FALSE will always be bitwise 0 (and No in CF). And TRUE will be bitwise 1, but also Yes and any non-0 number. So 42 is still TRUE. And -1 is still TRUE (special thanks MS Access Yes/No datatypes :-p).

Since FALSE is always 0 (or NO or FALSE), it's usually better to check for NOT FALSE or NEQ 0 than it is to check for TRUE. However, even with that, you still have watch out. Determine what you want to consider TRUE and do a full evaluation for that. ColdFusion sometimes casts TRUE and FALSE variables differently. Look at the results of the following code:

(thanks to Bert Dawson and Jamie Jackson for their querySim script.)

<!--- set up the fake query: --->
<cfscript>
/**
 * Accepts a specifically formatted chunk of text, and returns it as a query object.
 * v2 rewrite by Jamie Jackson
 * 
 * @param queryData      Specifically format chunk of text to convert to a query. (Required)
 * @return Returns a query object. 
 * @author Bert Dawson ([email protected]) 
 * @version 2, December 18, 2007 
 */
function querySim(queryData) {
    var fieldsDelimiter="|";
    var colnamesDelimiter=",";
    var listOfColumns="";
    var tmpQuery="";
    var numLines="";
    var cellValue="";
    var cellValues="";
    var colName="";
    var lineDelimiter=chr(10) & chr(13);
    var lineNum=0;
    var colPosition=0;

    // the first line is the column list, eg "column1,column2,column3"
    listOfColumns = Trim(ListGetAt(queryData, 1, lineDelimiter));

    // create a temporary Query
    tmpQuery = QueryNew(listOfColumns);

    // the number of lines in the queryData
    numLines = ListLen(queryData, lineDelimiter);

    // loop though the queryData starting at the second line
    for(lineNum=2;  lineNum LTE numLines;  lineNum = lineNum + 1) {
        cellValues = ListGetAt(queryData, lineNum, lineDelimiter);

        if (ListLen(cellValues, fieldsDelimiter) IS ListLen(listOfColumns,",")) {
            QueryAddRow(tmpQuery);
            for (colPosition=1; colPosition LTE ListLen(listOfColumns); colPosition = colPosition + 1){
                cellValue = Trim(ListGetAt(cellValues, colPosition, fieldsDelimiter));
                colName   = Trim(ListGetAt(listOfColumns,colPosition));
                QuerySetCell(tmpQuery, colName, cellValue);
            }
        } 
    }
    return( tmpQuery );
}
</cfscript>

<!--- populate the fake query --->    
<cfscript>
fakeQuery = querySim('
testID , isThisTruthy
1 | TRUE
2 | FALSE
3 | YES
4 | NO
5 | 1
6 | 0
7 | -1
8 | 42
');
</cfscript>
<!--- End of the fake query setup --->

<!--- Dump the fakeQuery so we can see what we've got. --->
<cfdump var="#fakeQuery#" label="fakeQueryInfo" />

<!--- 
    Not really necessary since the query is created above. Just included for 
    clarity, as everything above this line can really be ignored if connecting 
    to a real query. 
--->
<cfquery name="truthyCheck" dbtype="query">
    SELECT testID, isThisTruthy
    FROM fakeQuery
</cfquery>

<!--- Begin the truthy statements. --->
<br/><br/>
<strong>cfif isThisTruthy >></strong> 
<!--- 
    This one has an implicit evaluation of TRUE or FALSE that seems to be based on a
    broader (and more accurate) definition of what should be TRUE or FALSE. However,
    it's much less clear in what you're trying to do. 
--->
<br/>
<cfoutput query="truthyCheck">
    #testID#: #isThisTruthy# | <cfif isThisTruthy>True<cfelseif NOT isThisTruthy>False<cfelse>NULL</cfif> <br/>
</cfoutput>
<br/><br/>

<!--- 
    The rest of these appear to actually evaluate down to a bit (using the standard
    1,0,YES,NO,TRUE,FALSE definitions) and then they do an integer comparison. This 
    may not be completely what you're looking for.
--->
<strong>cfif isThisTruthy IS TRUE >></strong>
<br/>
<cfoutput query="truthyCheck">
    #testID#: #isThisTruthy# | <cfif isThisTruthy IS TRUE>True<cfelseif isThisTruthy IS NOT TRUE>False<cfelse>NULL</cfif> 
    <!--- 1 IS 1 IS TRUE, but -1 IS 1 IS FALSE. --->
    <br/>
</cfoutput>
<br/><br/>
<strong>cfif isThisTruthy EQ 1 >></strong>
<br/>
<cfoutput query="truthyCheck">
    #testID#: #isThisTruthy# | <cfif isThisTruthy EQ 1>True<cfelseif isThisTruthy NEQ 1>False<cfelse>NULL</cfif> 
    <!--- 1 EQ 1 IS TRUE, but -1 EQ 1 IS FALSE. --->
    <br/>
</cfoutput>
<br/><br/>
<strong>cfif isThisTruthy NEQ 0 >></strong>
<br/>
<cfoutput query="truthyCheck">
    #testID#: #isThisTruthy# | <cfif isThisTruthy NEQ 0>True<cfelseif isThisTruthy EQ 0>False<cfelse>NULL</cfif> 
    <!--- 1 NEQ 0 and -1 NEQ 0 both evaluate to the same. --->
    <br/>
</cfoutput>
<br/><br/>
<strong>cfif isThisTruthy NEQ FALSE >></strong>
<br/>
<cfoutput query="truthyCheck">
    #testID#: #isThisTruthy# | <cfif isThisTruthy NEQ FALSE>True<cfelseif isThisTruthy EQ FALSE>False<cfelse>NULL</cfif> 
    <!--- 1 NEQ 0 and -1 NEQ 0 both evaluate to the same. --->
    <br/>
</cfoutput>

You'll get:

cfif isThisTruthy >>
1: TRUE | True
2: FALSE | False
3: YES | True
4: NO | False
5: 1 | True
6: 0 | False
7: -1 | True <--- Technically correct
8: 42 | True <--- Technically correct

cfif isThisTruthy IS TRUE >>
1: TRUE | True
2: FALSE | False
3: YES | True
4: NO | False
5: 1 | True
6: 0 | False
7: -1 | False <--- Technically incorrect
8: 42 | False <--- Technically incorrect

cfif isThisTruthy EQ 1 >>
1: TRUE | True
2: FALSE | False
3: YES | True
4: NO | False
5: 1 | True
6: 0 | False
7: -1 | False <--- Technically incorrect
8: 42 | False <--- Technically incorrect

cfif isThisTruthy NEQ 0 >>
1: TRUE | True
2: FALSE | False
3: YES | True
4: NO | False
5: 1 | True
6: 0 | False
7: -1 | True <--- Technically correct
8: 42 | True <--- Technically correct

cfif isThisTruthy NEQ FALSE >>
1: TRUE | True
2: FALSE | False
3: YES | True
4: NO | False
5: 1 | True
6: 0 | False
7: -1 | True <--- Technically correct
8: 42 | True <--- Technically correct

like image 40
Shawn Avatar answered Nov 18 '22 16:11

Shawn