【问题标题】:ColdFusion "Yes" eq "True"; "No" eq "FalseColdFusion "是" eq "True"; "否" eq "假
【发布时间】:2013-03-19 08:10:25
【问题描述】:

为什么会这样?

<cfif "yes" eq "true">

Yes equals true.

<cfelse>

Yes does not equal true.

</cfif>

页面输出。 “是的就是真的。”

【问题讨论】:

标签: coldfusion


【解决方案1】:

Chris' 和 Keshav 的答案是正确的,ColdFusion 会转换这些值。这是ColdFusion数据转换的官方文档。

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"

【讨论】:

    【解决方案2】:

    ColdFusion 具有动态变量类型,并且在运行时会进行一些奇怪的转换,字符串可以用作布尔值、日期和数字而无需强制转换,这既有优点也有缺点。

    如果你需要做一个简单的字符串比较,你可以使用内置的 compare() 函数。

    Ben Nadel 在这里对字符串比较选项进行了很好的总结 - http://www.bennadel.com/blog/236-ColdFusion-String-Comparison-Compare-vs-Equals-vs-CompareTo-.htm

    【讨论】:

      【解决方案3】:

      您必须非常小心对真/假的比较。它们应该很简单,但有时,取决于您编写代码的方式,TRUE/FALSE 的评估可能会有所不同。根据几乎所有的定义,FALSE 总是按位为 0(在 CF 中为否)。 TRUE 将按位为 1,但也是 Yes 和任何非 0 数。所以 42 仍然是 TRUE。并且 -1 仍然是 TRUE(特别感谢 MS Access 是/否数据类型 :-p)。

      由于 FALSE 始终为 0(或 NO 或 FALSE),因此检查 NOT FALSE 或 NEQ 0 通常比检查 TRUE 更好。但是,即使这样,您仍然要小心。确定您要考虑的内容为 TRUE 并对此进行全面评估。 ColdFusion 有时会以不同的方式转换 TRUE 和 FALSE 变量。看下面代码的结果:

      (感谢 Bert Dawson 和 Jamie Jackson 的 querySim 脚本。)

      <!--- 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 (bert@redbanner.com) 
       * @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>
      

      你会得到:

      cfif isThisTruthy >>
      1:真|真
      2:假 |假的
      3:是 |真
      4:否 |假的
      5:1 |真
      6:0 |假的
      7:-1 |正确
      8:42 |正确

      cfif isThisTruthy IS TRUE >>
      1:真|真
      2:假 |假的
      3:是 |真
      4:否 |假的
      5:1 |真
      6:0 |假的
      7:-1 |错误
      8:42 |错误

      cfif isThisTruthy EQ 1 >>
      1:真|真
      2:假 |假的
      3:是 |真
      4:否 |假的
      5:1 |真
      6:0 |假的
      7:-1 |错误
      8:42 |错误

      cfif isThisTruthy NEQ 0 >>
      1:真|真
      2:假 |假的
      3:是 |真
      4:否 |假的
      5:1 |真
      6:0 |假的
      7:-1 |正确
      8:42 |正确

      cfif isThisTruthy NEQ FALSE >>
      1:真|真
      2:假 |假的
      3:是 |真
      4:否 |假的
      5:1 |真
      6:0 |假的
      7:-1 |正确
      8:42 |正确

      【讨论】:

      【解决方案4】:

      在 ColdFusion 中,"yes" 和 "true" 在用作表达式时都转换为数值 1

      【讨论】:

        【解决方案5】:

        是的,1 和 true 在coldFusion 中是一回事。如此链接所示:

        http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec09af4-7fd0.html

        【讨论】:

          【解决方案6】:

          在coldfusion中,“yes”和“true”被解释为bit(1),“no”和“false”被解释为bit(0),所以“yes”和“true”是相等的。同样的方式

          <cfif 1 eq "true">
          
          Yes equals true.
          
          </cfif>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-12-20
            • 1970-01-01
            • 1970-01-01
            • 2018-05-15
            • 2013-02-10
            • 1970-01-01
            相关资源
            最近更新 更多