Skip to main content
Known Participant
April 5, 2011
Question

Best way to check for variable

  • April 5, 2011
  • 2 replies
  • 535 views

Hello,

very newbie question,

what is the best way to check for submitted variables?

I'm using

<cfparam name="url.id" default="">

<cfif url.id neq "">

But I've seen also other ways, such as isdefined, or structkeyexists. But I guess these later two return true also in case of index.cfm?id=

Thanks!

This topic has been closed for replies.

2 replies

Participating Frequently
April 7, 2011

<cfparam name="url.id" default="">

<cfif url.id neq "">

In your scenario I guess you do not need to use IsDefined() or StructKeyExists() functions as you are using cfparam to declare and initialize the "url.id" variable.

Added to that I will also recommend to the below:

<cfif url.id neq ""> can also be done as <cfif Len(url.id)>

ilssac
Inspiring
April 5, 2011

YES they would.  As you have noted, there is a difference between the EXISTENCE of a variable and the VALIDITY of a variable's value.

You are checking the validity of the value (not an empty string).  Sometimes that is not necessary, sometimes it is.  The best code is what is necessary for your project.