stringcoldfusionbooleancoldfusion-8

Coldfusion string == true OR empty == false?


I am used to using PHP and JavaScript but I have now begun working on a project in Coldfusion.

In PHP I am used to a string being "truthy" and empty/null being "falsy".

This doesn't seem to hold true with ColdFusion (specifically v8).

I want to make the following work but cannot figure out how to make CF see the string as truthy:

<cfset x = "path\to\something.cfm">
<cfif x>
    x is truthy
<else>
    x is falsy
</cfif>

I always get the error: cannot convert the value "path\to\something.cfm" to a boolean


Solution

  • ColdFusion has some similar "truthiness" functionality to it.

    The following will evaluate to true

    The following will evaluate to false

    In CF we generally use the len() function to determine if a string has anything in it. Since a non-zero number evaluates to "true" this works.

    Your pseudo-code would be, then:

    <cfset x = "path\to\something.cfm">
    <cfif len(x)>
        x is truthy
    <else>
        x is falsy
    </cfif>
    

    Since ColdFusion converts nulls to empty strings, using trim() in conjunction would be a good idea, like so: <cfif len(trim(x))>.

    There is no isString() function, but there is isValid(): isValid("string",x)

    YesNoFormat() simply turns a boolean value into a nicely formatted "Yes" or "No".