Search code examples
coldfusioncoldfusion-10

ColdFusion Script Passing 2 variables


I am trying to return two variables in a ColdFusion function.

I know you can do this in C++ using the & sign.

my code:

<cfscript>

function browserDetect(browser,version) {

      browser="some value string";
      version="some other value string";
}
</cfscript>

other page:

<cfoutput>#BrowserName# and #BrowserVer#</cfoutput>

Solution

  • My preferred method is to return back a struct with the appropriate key/value pairs.

    <cfscript>
    
        function browserDetect(arg1,arg2) {
          var resultStruct = structNew() ;
          resultStruct.browser="some value string";
          resultStruct.version="some other value string";
    
          return resultStruct ;
        }
    </cfscript>
    

    Instead of creating a local struct, you could also return back the results using an explicit struct

    return { browser : "some value string" , version : "some other value string" }
    

    Set resultVar to the results of your function and then you would then just output using

    <cfoutput>#resultvar.browser# and #resultVar.version#</cfoutput>
    

    You could also create a JSON string and return that back .