CFLib.org – Common Function Library Project

IsEmpty(varName)

Last updated July 10, 2003

author

Fabio Serra

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
Check if a variable is set and has a value. This UDF will check to see if the variable is an array, structure, or query. If so, it will check to see if any data exists in the variable, and if not, will return true.

Return Values:
Returns a boolean.

Example:

<cfset myVar = 1>
<cfset myVar3 = arrayNew(1)>
<cfset myVar4 = arrayNew(1)>
<cfset myVar4[1] = "e">
<cfset myVar5 = structNew()>
<cfset myVar6 = structNew()>
<cfset myVar6.name = "ray">
<cfset myVar7 = queryNew("F")>
<cfset myVar8 = queryNew("F")>
<cfset queryAddRow(myVar8,1)>

<cfoutput>
IsEmpty("myvar") = #isEmpty("myVar")#<br>
IsEmpty("myvar2") = #isEmpty("myVar2")#<br>
IsEmpty("myvar3") = #isEmpty("myVar3")#<br>
IsEmpty("myvar4") = #isEmpty("myVar4")#<br>
IsEmpty("myvar5") = #isEmpty("myVar5")#<br>
IsEmpty("myvar6") = #isEmpty("myVar6")#<br>
IsEmpty("myvar7") = #isEmpty("myVar7")#<br>
IsEmpty("myvar8") = #isEmpty("myVar8")#<br>
</cfoutput>

<cfif isEmpty("myVar")>
<cfset myVar = "Pippo">
</cfif>

Parameters:

Name Description Required
varName Variable to check for. Yes

Full UDF Source:

/**
 * Check if a variable is set and has a value.
 * Mods by RCamden to add support for struct/query
 * 
 * @param varName      Variable to check for. (Required)
 * @return Returns a boolean. 
 * @author Fabio Serra (faser@faser.net) 
 * @version 1, July 10, 2003 
 */
function isEmpty(varName) {
    var ptr = "";
    
    if(not isDefined(varName)) return true;
    ptr = evaluate(varName);
    
    if(isSimpleValue(ptr)) {
        if(not len(ptr)) return true;
    } else if(isArray(ptr)) {
        if(arrayIsEmpty(ptr)) return true;
    } else if(isStruct(ptr)) {
        if(structIsEmpty(ptr)) return true;
    } else if(isQuery(ptr)) {
        if(not ptr.recordCount) return true;
    }
        
    return false;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson