cfparam with unscoped variables

January 4, 2011 by Mike Henke    2 Comments
Posted under: 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.

2 Comments + Add Comment

  • spills

    Had a similar issue just come up recently and now I make sure to "scope" my params, i.e.;

    param name="url.mike" default="";.

    I wish your article came out sooner to help me but its great to document behavior like this because it is the "simple" things that often are overlooked and are most irritating to diagnose.

    Thanks for your time for writing this.

  • Mike Henke

    Thanks for the kind words.

  • Got anything to say? Go ahead and leave a comment!

  • Adobe Community Professionals
    yeomanly
    • Google Translate