cfparam with unscoped variables

ColdFusion ColdFusion

I thought an unscoped parameter when using CFPARAM would be placed into the variables scope if the parameter didn't exist in the variables scope, similiar to CFSET when no scope is defined. Today I realized, CFPARAM doesn't always set an unscoped parameter to the variables scope. Here is a code example to explain better.

Code Example

So when you run this the page should see something like this.

Code Results

Everything Cool? Now, add to the url in the address bar ?mike=cool and reload the page. Error, What happenned?

Error Results

Well, it seems variables.mike is undefined because CFPARAM is similar in regards to how ColdFusion evaluates an unscoped variable. CFPARAM tests for the existence of an unscoped parameter in every scope. Since in our error example, mike parameter is already defined, cfparam finds it and drives on. In the first example, mike parameter doesn't exist in any scope, so cfparam provides mike in the variables scope.

Mike Henke

Written by Mike Henke

Mike Henke (pronounced "hang-kee") is a full-stack developer with 25+ years of experience, evolving from ColdFusion to modern frontend frameworks, AI, and cloud technologies. Based in Omaha, he enjoys time with his wife and three children when not coding.