Skip to main content
Ben Nadel at cf.Objective() 2013 (Bloomington, MN) with: Gert Franz and Aaron Greenlee
Ben Nadel at cf.Objective() 2013 (Bloomington, MN) with: Gert Franz Aaron Greenlee

IsNull() vs. IsDefined() For ColdFusion 9 Scope Detection

By
Published in Comments (2)

I just spent an hour trying to debug a ColdFusion 9 problem when I realized that the root of the problem was the fact that ColdFusion 9's new IsNull() method and the previously existing IsDefined() method work very differently for scope detection. While the built-in ColdFusion scopes might not seem like something you ever need to test for, remember that the FORM scope doesn't actually exist during SOAP-based requests.

To see the difference in scope-detection using both IsNull() and IsDefined(), take a look at this demo:

<!---
	Check to see if the URL and FORM scopes exist using
	both the new isNull() and the old isDefined() methods.
--->
<cfset check = {} />

<!--- Check for URL scope. --->
<cfset check.isUrlNull = isNull( url ) />
<cfset check.isUrlDefined = isDefined( "url" ) />

<!--- Check for FORM scope. --->
<cfset check.isFormNull = isNull( form ) />
<cfset check.isFormDefined = isDefined( "form" ) />


<!--- Check for scope existence. --->
<cfdump
	var="#check#"
	label="In-Page Check"
	/>

Here, we are simply checking the URL and FORM scope existence using IsNull() and IsDefined(). When we run the above code, we get the following output:

IsNull() vs. IsDefined() For ColdFusion 9 Scope Detection.

As you can see, all of them report back YES. Of course, a scope cannot be both NULL and Defined at the same time - that would be like dogs and cats living together - it just can't happen.

From what I have seen and heard before, I believe that IsNull() gets converted to StructKeyExists() at compile time; as such, it would make sense that it requires some sort of scoped-value testing. I think it's probably using whatever the local scope is at run time (ie. local? variables?). Anyway, as much as I love the ColdFusion 9's new IsNull() function, it looks like there are still a few valid times to use the IsDefined() function.

Want to use code from this post? Check out the license.

Reader Comments

1 Comments

I experienced this very thing today! Checking to see if a variable is defined in the URL or not, ended up using ISDEFINED() and it works perfectly for me.

BTW, my 3 cats and 2 dogs live just fine together! :)

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel