AdobeStock_455007340

And Using Functions as the DEFAULT

Home » And Using Functions as the DEFAULT

This one has tripped me up a few times recently, so I thought it best to share. is used (among other things) to assign default values, for example . You can use CFML functions for the default value, but you must provide the expression as a string with #’s delimiting the function call. For example, will not assign the current year to variable yy, rather, yy will contain the string “Year(Now())”. While neither intuitive nor consistent, the correct syntax is: .

4 responses to “And Using Functions as the DEFAULT”

  1. seancorfield Avatar
    seancorfield

    If you assume "XHTML compliance" then all attributes will be "quoted" so you”d have to use the second (correct) form. The only tags where this isn”t realistic to apply are: CFIF, CFSET and CFRETURN (I think… are there others?).

  2. Ben Forta Avatar
    Ben Forta

    Sean, true, but CFML is not "XHTML compliant", and thus my comment about it being inconsistent. After all, <CFPARAM NAME="x" DEFAULT="1"> is the equivelant to <CFIF NOT IsDefined("x")><CFSET x=1></CFIF>. Also, attributes passed to custom tags, <CFMODULE>, and others, need not be quoted.

  3. Raymond Camden Avatar
    Raymond Camden

    Something else to remember, everytime you run cfparam, if you use "#somefunc()#" it will execute EVERY time, even if the variable you are checking for already exists.

  4. Igor Ilyinsky Avatar
    Igor Ilyinsky

    Ray, I don’t get it…
    If it executes every time, then why use cfparam instead of cfset. Am I misunderstanding or ar you saying that CFPARAM will execute the function without regard to whether the variable is already set?
    -Igor

Leave a Reply