Can you scatter several variables at once in ColdFusion?

While this is possible and not causing an error, I would like to know if the approved practice is var for several variables on the same line as follows:

<cfset VAR var1 = var2 = var3 = ''> 

I would appreciate all ideas and opinions, especially if they come with documentation as an argument of support. I know that it works without causing an error, but I cannot find specific documentation about whether it will have a variable or if it simply declares a value.

Thanks!

+4
source share
5 answers

As everyone has been told, you need to combine your variables with var or local . Also, as @Ben said, you cannot really var variables like you have. I would suggest using a local scope and doing something like the following:

 <cfscript> var val = 'some value' local = { var1 = duplicate(val), var2 = duplicate(val), var3 = duplicate(val) }; </cfscript> 

This, in my opinion, is the fastest way to achieve what you seem to be doing. I use a duplicate function, so if you use a complex variable as the value of val (struct, array, etc.), you will not run into a problem with links.

+2
source

FWIW, yes, you can do it in CF9:

 <cfset var a = var b = c = d*5> 

Here is the documentation that gives this example. In this case, I assume that the variables c and d have already been defined earlier in this function.

So, you need to repeat the var keyword for each variable, and in your case this will result in:

 <cfset VAR var1 = VAR var2 = VAR var3 = ''> 
+3
source

<cfset> does not contain scope variables.

var variables require the keyword var

<cfset var someVariable = 1>

Otherwise, variables will be created, but default areas will be assigned. The default scope depends on the context and on the version of CF that we are talking about.

For CFC methods:

  • CF9 → LOCAL scope
  • CF8 → VARIABLES region

Due to scope rules in CF9, the var keyword is not really required. Variables can be added to the LOCAL area directly and at any time ( <cfset LOCAL.foo = "bar" ). There is no need for their coverage ahead of schedule.

+1
source

I suppose I should start by pointing out that your code does not change the variable variables, but simply sets their values. You should use the VAR keyword (or LOCAL area in CF9) to indicate that the variables should have a VAR.

In addition, chain assignment does not work in all versions of CF. My workplace is still at 6.1, and this is causing an error.

Finally, if I remember correctly, if you use the assignment operator as described above, var1 and var2 are true, since the result of the assignment operation is true if no error has occurred. (someone will correct me if I am wrong, since I can not check it at work, since we are at 6.1)

In any case, in order to return to your question, I do not associate an appointment or a show. First, I think individual assignments are clearer. secondly, it does not work in all versions. Therefore, I would suggest that preferences be preferred.

+1
source

I have seen people just create a var structure and define your variables as keys for the structure. This way you do not need to constantly use var. For instance:.

 <cfset var local = structNew() /> <cfset local.var1 = "" /> <cfset local.var2 = "" /> 

This is useful for those who use ColdFusion Server 8 and below and get stuck with the var scope.

0
source

All Articles