Skip to main content
2Charlie
Inspiring
Mar 8, 2016 5:42 PM UTC
Answered

Can I access a function variable form outside?

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?

    This topic is closed to new replies. Start a new post to keep the conversation going.
    EddieLotter
    EddieLotterCorrect answer
    Inspiring
    Mar 8, 2016 7:14 PM UTC

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

    Cheers

    Eddie

    2 replies

    EddieLotter
    EddieLotterCorrect answer
    Inspiring
    Mar 8, 2016 7:14 PM UTC

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

    Cheers

    Eddie

    Translate
    Carl Von Stetten
    Legend
    Mar 8, 2016 9:40 PM UTC

    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.

    Translate