Variable Scope Made Simple

Recently asked on Yahoo! Answers:

What does this statement mean? “Local variables are local to a function or a procedure.”?

What it means, in a nutshell, is if you declare a variable inside a function or procedure, you cannot get the value of that variable from outside that function or procedure.

Local Scope To A Single Function Or Procedure

For example, consider this PHP code:

//function with variable local to the function
function function1() {
$test1 = "Hello World!";
return $test1;
}

//output values
echo "value of test1 variable: $test1 <br />";
echo "value of function1: " . function1() ."<br />";

In the case above, we declare the variable $test1 inside the function named function1. So, it is “local” to the function; it doesn’t exist until the function creates it, and once the function is done doing its business, the variable no longer exists.

That is, $test1 has “scope”, or “lifetime”, only within function1. It’s born in function1, dies in function1, and doesn’t ever expose itself to anything outside function1.

That’s why, if you execute the script above, you get the following output:

value of test1 variable:
value of function1: Hello World!

Because $test1 doesn’t have scope outside of function1, any attempt to call $test1 from outside that function will result in null. But since it does have scope inside function1, and we’ve set a value for it inside function1, we can get its value while we’re inside function1.

Local Scope To A Container, But Not To A Function Or Procedure Inside That Container

You can also have a condition where a variable is declared in a function, procedure or other container, but is not “local” to functions or procedures called from within that container.

Consider this code:

//function with variable local to a container,
//but not local to the function called from within that container
$test2 = "Hello World!";

function function2() {
return $test2;
}

$test2a = $test2;

//output variables
echo "value of test2 variable: $test2 <br />";
echo "value of function2: " . function2() ."<br />";
echo "value of test2a variable: $test2a <br />";

In the case above, we create a variable named $test2 and provide it a value, but we don’t pass it as an argument to function2. As a result, $test2 doesn’t have scope in function2. But because it has scope within the originating container, we can create a new variable after we call function2, and assign that new variable a value based on the value of $test2.

So, when our results are echoed out, we get this:

value of test2 variable: Hello World!
value of function2:
value of test2a variable: Hello World!

Since $test2 and $test2a belong to the same container — that is, the same function or procedure — they see each other, and can work with each other. But function2 is a whole different container; since we don’t supply function2 with any arguments, it can’t see any of the variables in its parent container. So, if we try to call $test2, we get null.

Global Variables, Available To All Items In The Code

A third example of scope / lifetime is a global variable. Globals are declared with the specific intent that we can call it from anywhere within the script. It’s available to any function or procedure that calls it.

Look at this PHP code:

//global variable
global $test3;
$test3 = "Hello World!";

function function3() {
global $test3;
return $test3;
}

//output variables
echo "value of test3 variable: $test3 <br />";
echo "value of function3: " . function3() ."<br />";

The output we get from that script is this:

value of test3 variable: Hello World!
value of function3: Hello World!

You can see all this in action at http://demo.dougv.com/localvariables/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Check out the Commenting Guidelines before commenting, please!
  • Want to share code? Please put it into a GitHub Gist, CodePen or pastebin and link to that in your comment.
  • Just have a line or two of markup? Wrap them in an appropriate SyntaxHighlighter Evolved shortcode for your programming language, please!