Re: Re: [PHP] Local variable protection

From: Date: Fri, 14 Oct 2011 16:24:00 +0000
Subject: Re: Re: [PHP] Local variable protection
Groups: php.general 
Request: Send a blank email to php-general+get-315300@lists.php.net to get a copy of this message
On 14 Oct 2011 at 16:46, Tedd Sperling <tedd.sperling@gmail.com> wrote: > On Oct 13, 2011, at 11:37 AM, Tim Streater wrote: >> On 13 Oct 2011 at 16:25, Tedd Sperling <tedd.sperling@gmail.com> wrote: >>> So, if you want a main script variable (i.e., $myVar) to be accessed by a >>> function, you can do it by stating: >>> >>> myFunction >>> { >>> global $myVar; >>> // and then using $myVar >>> } >>> >>> or >>> >>> myFunction >>> { >>> $myVar = $GLOBAL['myVar'] >>> // and then using $myVar >>> } >> >> But presumably these are not *quite* equivalent, as modifying $myVar will >> change the global in the first but not in the second. >> >> -- >> Cheers -- Tim > > Tim: > > I see what you are saying, but the reason for that $myVar declared within the > function is local to that function and will not change the value of $myVar in > the main script -- as such, illustrating differences in scope. Yes. > But the reason for my post was to illustrate that IF one declares a variable > in the main script THEN that variable will also be automagically included in > the $GLOBAL array. > > In short, you cannot write a script without having a $GLOBAL array that > contains every variable you create in the main script -- that is what I found > surprising. YSMV (Your Surprise May Vary). I suppose my reaction is more like hmmm, interesting. I use globals here and there, e.g. to keep argument lists from getting very long. But I never use the $GLOBAL array. -- Cheers -- Tim

« previous php.general (#315300) next »