What's the difference between these two blocks of code, when called right after a cffunction tag:
<cfparam name="bork_bork_bork" default="false">
<cfargument name="bork_bork_bork" required="false" default="false">
What's the difference between these two blocks of code, when called right after a cffunction tag:
<cfparam name="bork_bork_bork" default="false">
<cfargument name="bork_bork_bork" required="false" default="false">
cfparam has nothing to do with functions. I can see that this is confusing, given that param/argument are interchangeable words in most languages. Keep in mind that user-defined functions weren't added to CF until version 5, so there was no conflict in using cfparam as a way to initialize variables. Moreover, the cfparam tag probably drew its name from the now obsolete function, ParameterExists() (or vice versa - by the time I got into CF, at version 4.0 (1999), that function was already deprecated, so I missed the history behind it)
cfparam is a way to set a default for any variable if the variable doesn't already exist. It's a shortcut to do the following:
<cfif NOT isDefined('bork_bork_bork')>
<cfset bork_bork_bork = 'myDefaultSetting'>
</cfif>
cfargument is only useable after a opening tag to define an argument being passed to a CFC function or a user-defined function.
From what I recall, nothing can exist between the cffunction tag and cfargument tag, so they must appear right after the cffunction tag.
From within the function you'll access cfargument via the arguments scope {arguments.bork_bork_bork} or via an array {arguments1}
cfparam will just ensure the variable is available on the request is should not be used instead of cfargument. For further reading check out:
<cfparam>
, when used with the default attribute, will ensure that a variable exists. Because there is no scope specified,bork_bork_bork
is being put in to theVariables
scope.<cfargument>
is used to pass an arguments to a function. These values are stored in theArguments
scope. You would access the value usingarguments.bork_bork_bork
.Note that
arguments.bork_bork_bork
andbork_bork_bork
are not the same. The scope ofarguments
is only within the function, the other is being stored in theVariables
scope and will be valid anywhere on the page (though I would not recommend coding it that way.)