Return to Snippet

Revision: 45420
at April 29, 2011 22:30 by nehringryan


Initial Code
Need to make sure a variable exists in Coldfusion? Use one of the options below to first check for a value passed from the server/form/whatever.  If none is sent, for any reason whatsoever (var error, db error, net hiccup etc...), this code  will ensure you still have a variable with a valid defaulted value to ensure 1 problem doesn't become 2.


	<!--- Example of Coldfusion's IsDefined Function --->

		<!--- This first way is probably easer to understand to newer 								programmers or people coming from other languages to CF. --->
		
		<!--- Check if the form sent in a value, and if it didn't, then manually 				set it. Nothing too mindblowing huge here. --->

	  <cfif isDefined("Form.firstname")>
	    <cfoutput>Hello, #Form.firstname#!</cfoutput>
	  <cfelse>
	    <cfoutput>Hello, stranger!</cfoutput>
	  </cfif>

	<!--- Example of Coldfusions <cfparam> tag used instead --->

		<!--- Using cfparam tag is evern easier, less code & more the 
		technically right way do this in CF (maybe?... lol )  ) --->
	 
  		<cfparam name="Form.firstname" default="stranger">
		<cfoutput>Hello, #Form.firstname#!</cfoutput>
		

One clean, little, and simple to remember Coldfusion tag replaces a "CFIF"      conditional process & all the declararative code you'd need to set those values by hand.This is a pretty small example, but the implications are pretty apparent if you had to check maybe 20 variables etc.

Initial URL
http://ryannehring.com/

Initial Description

                                

Initial Title
Coldfusion Variable Checking / Setting Default

Initial Tags

                                

Initial Language
ColdFusion