CFLib.org – Common Function Library Project

forceBoolean(bool)

Last updated September 5, 2010

Version: 1 | Requires: ColdFusion 5 | Library: UtilityLib

 
Rated 1 time(s). Average Rating: 1.0

Description:
You pass in a variable, and I will return either a "1" if it evaluates to true, or a "0" if it is not a boolean (as in an empty string or any other non-boolean) or evaluates to false.

Return Values:
Returns a boolean.

Example:

view plain print about
<cfset i="">
<cfif i></cfif> returns error.
<ciff forceBoolean(i)></cfif> works.
#forceBoolean(i)# returns "0".

Parameters:

Name Description Required
bool Value to check. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Returns a boolean 1/0 from anything, including empty strings.
 * 
 * @param bool      Value to check. (Required)
 * @return Returns a boolean. 
 * @author Alan McCollough (amccollough@anthc.org) 
 * @version 1, September 5, 2010 
 */

function forceBoolean(bool){
  if(isBoolean(bool) AND bool IS TRUE)
     return "1";
  else
     return "0";
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
3 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson