Ensuring variable existence

ColdFusion generates an error if you try to use a variable value that does not exist. Therefore, before you use any variable whose value is assigned dynamically, you must ensure that a variable value exists. For example, if your application has a form, it must use some combination of requiring users to submit data in fields, providing default values for fields, and checking for the existence of field variable values before they are used.

There are several ways to ensure that a variable exists before you use it, including the following:

  • You can use the IsDefined function to test for the variable’s existence.

  • You can use the cfparam tag to test for a variable and set it to a default value if it does not exist.

  • You can use a cfinput tag with a hidden attribute to tell ColdFusion to display a helpful message to any user who does not enter data in a required field. For more information on this technique, see Requiring users to enter values in form fields.