Php - Scope of Variables
We just introduced functions, now along with functions comes Scope of variables. In common day terms, let me try to explain scope. If you have 4 tvs in different rooms, with 4 cables box. Then each tv can show a
different channel, however, if you have 4 tvs, and 3 cables boxes and 2 tvs sharing a cable box. Then you will only have 3 different shows. So that 2 tvs share the same show and the other 2 tvs, showing their own shows. That is how scope works..
Global Scope
Variable scope becomes important, when you have 2 variables with the same name, as can easily happen within large application. It also ocurrs when you write functions to perform specific tasks.
This means that no matter where you are in the programs code, this variable will be known and have the same value everywhere. So in our tv example, this means that all 4 tvs, are using the same cable box, so when you change a channel on any tv, all the other tvs will change channel as well.
Global variables, can be useful, but i have been taught, and i continue to believe, that where possible you should stay away from global variables. In most cases, they are different ways to getting around global variables, but other times, you just have to use them.
Local Scope
Variables defined in local scope are only available within they “block”, function in which they are defined. So outside of the function or block, these variables will have no value, and will be unknown.
Within the scope they are defined, if they happen to be defined in another scope, then changes made to the variable in one place will not affect what happens in the other function. Back to the tv scenario again, 2 cables box, no matter what you do with one box, unless you go and change the other box, the changes on the first box will not make a difference.
Static Variables
Variables declared in a function, are defined everytime a function is called. So that if the function is called and the variable is defined to be zero and the variable was modify to be 10,when the function is called a
second time, the variable goes back to being zero when the function is called.
Static variables defined in a function, remember the value of the variable, when it was modified and uses that value everytime the function is called. So in actual fact, a static variable within a function, always remember what it value is, so that when the function is called again, it just continues from where it left off, and does not start again from the declaration.
Examples Of Scope
< ?php
$index = 0;
$teacher = "Joe Pulie";
function print_names ($index, $newchild)
{
static $counter = 9;
if ( $newchild == "yes")
$counter++;
$teacher = "Juan Guinn";
print "\nMy teacher name is $teacher\n"."
“;
for ($index; $index< =$counter; $index++)
{
print "\nI am child $index out of $counter children\n"."
“;
}
global $teacher;
print “\nMy teacher name is $teacher\n”.”
“;
}
print “” .” \nHello Testing Scope\n”.”
“;
print_names($index,”no”);
$index = 9;
print “
“;
print_names(9,”yes”);
print “
“;
$bindex = 5;
print_names(5,”yes”);
print “
“;
?>
Hello Testing Scope
My teacher name is Juan Guinn
I am child 0 out of 9 children
I am child 1 out of 9 children
I am child 2 out of 9 children
I am child 3 out of 9 children
I am child 4 out of 9 children
I am child 5 out of 9 children
I am child 6 out of 9 children
I am child 7 out of 9 children
I am child 8 out of 9 children
I am child 9 out of 9 children
My teacher name is Joe Pulie
My teacher name is Juan Guinn
I am child 9 out of 10 children
I am child 10 out of 10 children
My teacher name is Joe Pulie
My teacher name is Juan Guinn
I am child 5 out of 11 children
I am child 6 out of 11 children
I am child 7 out of 11 children
I am child 8 out of 11 children
I am child 9 out of 11 children
I am child 10 out of 11 children
I am child 11 out of 11 children
My teacher name is Joe Pulie
Just is just touching the surface of scope, if you have any more questions, feel free to send me an email.


