Mar 03

It's such a simple thing but I actually googled this because I couldn't think of it offhand.

CODE:
  1. <cfif len(str)>
  2.      <cfoutput>#str#</cfoutput>
  3. </cfif>

if str has zero characters, len(str) will return 0, mimicking behaviour of false. Cool!

Popularity: 27% [?]

Share and Enjoy:
  • del.icio.us
  • Digg
  • Reddit
  • TwitThis
  • Google Bookmarks
  • StumbleUpon
  • Facebook
  • E-mail this story to a friend!
  • Print this article!

3 Responses to “ColdFusion Checks String Length”

  1. Joshua Curtiss Says:

    You'll find this is a common shortcut in most langs, actually. Of course, some argue it is poor practice because it is less readable; others say it is better because it is less code.

    Here's another cool thing you can do:

    The alternative would be a clumsy if statement to do the assignment.

  2. Joshua Curtiss Says:

    I was wondering if I needed to escape the code.. Here's the code snippet:

    <cfset IsItBig = (MyNum GT 10000)>

  3. Ken Miner Says:

    Would be a bad way to try to code. Try to create a function which you could then use anywhere on that page. The example below is a very easy way of doing this. Also you could put this business logic into a cfc and then use it anywhere on the site. Hope it helps some....

    local = structNew(); // KM: create a local structure

    function fnt_isItBig(myMun) {

    local.rtn_fntValue = false; // declare the variable that will be returned
    local.checkValue = 100000; // decalre the variable to check value against

    if ( local.myNum GT local.checkValue ) {

    local.rtn_fntValue = true; // it was greater
    }

    return local; // it was less than
    }

    local.myNum (#local.myNum#) is greater than #local.checkValue#

    local.myNum (#local.myNum#) is less than #local.checkValue#

Leave a Reply




  • Meta