ColdFusion Checks String Length
ColdFusion March 3rd, 2008It's such a simple thing but I actually googled this because I couldn't think of it offhand.
CODE:
-
<cfif len(str)>
-
<cfoutput>#str#</cfoutput>
-
</cfif>
if str has zero characters, len(str) will return 0, mimicking behaviour of false. Cool!
Popularity: 27% [?]









March 3rd, 2008 at 9:32 am
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.
March 3rd, 2008 at 9:33 am
I was wondering if I needed to escape the code.. Here's the code snippet:
<cfset IsItBig = (MyNum GT 10000)>
April 29th, 2009 at 12:14 pm
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#