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
Php Variable Scope
Started by
lazijunkie
, Jul 15 2006 04:07 AM
6 replies to this topic
#1
Posted 15 July 2006 - 04:07 AM
#2
Posted 15 July 2006 - 10:26 AM
Print is more than 50% slower than echo.
Edited by Cliff, 15 July 2006 - 10:42 AM.
#3
Posted 15 July 2006 - 10:07 PM
allright thanks, but it the demonstration of scope good?
#4
Posted 16 July 2006 - 07:28 AM
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
Sorry if that doesn't quite make sense it does to me but im abit hungover this morning lol
#5
Posted 19 July 2006 - 11:57 AM
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
with use of descriptive names.
I have written about naming standards as well
#6 _*-cherries-_*
Posted 03 August 2006 - 03:50 AM
instead of using "yes" and "no" why not use true and false?
#7
Posted 19 February 2007 - 05:01 PM
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 .
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users