Highlighted

Can I access a function variable form outside?

Engaged ,
Mar 08, 2016

Copy link to clipboard

Copied

I don't know much about ColdFusion yet. If I declare/cfset a variable inside a function, can I just access that variable directly from anywhere on the .CFM page?

If you set a variable in a function without specifying var then yes, the variable will become globally accessible.

Cheers

Eddie

Views

178

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Can I access a function variable form outside?

Engaged ,
Mar 08, 2016

Copy link to clipboard

Copied

I don't know much about ColdFusion yet. If I declare/cfset a variable inside a function, can I just access that variable directly from anywhere on the .CFM page?

If you set a variable in a function without specifying var then yes, the variable will become globally accessible.

Cheers

Eddie

Views

179

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mar 08, 2016 0
Advocate ,
Mar 08, 2016

Copy link to clipboard

Copied

If you set a variable in a function without specifying var then yes, the variable will become globally accessible.

Cheers

Eddie

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 08, 2016 0
Most Valuable Participant ,
Mar 08, 2016

Copy link to clipboard

Copied

As Eddie said, if you don't specify "var myVariable" (or, alternatively if on CF9+, "local.myVariable"), than the variable is globally accessible.  However, this is generally to be avoided as it breaks the principle of encapsulation.  Generally, a function shouldn't pollute the environment around it.  And not scoping variables to remain local to a function can result in race conditions or unintended behavior.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 08, 2016 0