Help - Search - Members - Calendar
Full Version: Php Variable Scope
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
lazijunkie
Php - 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 thought, 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.




Now back in the main program, I do a calls to the function

print "<b>" ." \nHello Testing Scope\n"."<br><br></b>";
print_names($index,"no");
$index = 9;
print "<br>";
print_names(9,"yes");
print "<br>";
$bindex = 5;
print_names(5,"yes");
print "<br>";






Please let me know what you think about this.and if it helps with scope
Ruben K
Print is more than 50% slower than echo.
lazijunkie
allright thanks, but it the demonstration of scope good?
Arutha
I thought your describtion of scope was abit iffy. I would have said that because large programs are coded by large grps of coders they split off features onto different people and they just get told what comes into it and what they want out off it and then all these functions/classes are pieced together into a working program at the end therefore because you arn't working with the other coders all the time you don't know what they are setting there variable names as, then agian i have always found if you use a prefix on the variables in a function/class you can avoid these problems.

Sorry if that doesn't quite make sense it does to me but im abit hungover this morning lol
lazijunkie
that is why is it is not a good idea to use global variables and have a name standard, that is easy to follow.
with use of descriptive names.
I have written about naming standards as well
-cherries-
instead of using "yes" and "no" why not use true and false?
Arutha
QUOTE(-cherries- @ Aug 3 2006, 08:50 AM) *
instead of using "yes" and "no" why not use true and false?


or 1 and 0. a good read for anyone considering a large PHP project with a group, global and local variables tend to be something a group off coders need to worry about more than a single coder, but thats why each coder should use an individual prefix for there variables ;).
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.