Coldfusion ignore undefined variables

if i use

<cfoutput>#somevariable#</cfoutput> 

and somevariable not defined. I am getting an error, how can I prevent an error due to occuration? Is there a simple way to implement a conditional that does not require a bunch of extra lines?

+4
source share
2 answers
 <cfparam name="somevariable" default="" /> 

If you are on cf 9, you can use the triple operation, but cfparam is more "best."

 #isDefined("somevariable") ? somevariable : 'default string'# 
+11
source

You can check the variable

 <cfoutput> <cfif isDefined("somevariable")> #somevariable# <cfelse> handle default scenario here </cfif> </cfoutput> 

or you can use the built-in conditional

 <cfoutput> #IIF(isDefined("somevariable"),de(somevariable),de(""))# </cfoutput> 
+1
source

Source: https://habr.com/ru/post/1334574/


All Articles