Re: Re: [PHP] Local variable protection
| From: | Tim Streater | Date: | Thu, 13 Oct 2011 15:37:00 +0000 |
| Subject: | Re: Re: [PHP] Local variable protection | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-315273@lists.php.net to get a copy of this message | ||
On 13 Oct 2011 at 16:25, Tedd Sperling <tedd.sperling@gmail.com> wrote:
> So, if in your main script you have the statement:
>
> $myVar = 'test';
>
> Then the $GLOBAL['myVar'] has also been created and will hold the value of
> 'test' without any additional coding.
>
> While many of you will say "But of course, that's the way it works." I
> actually said "What?!?" You see, I seldom use globals in my scripts and this
> runs counter to my 'keep the globals to an absolute minimum' practice. So
> while I was thinking my scripts didn't have globals, it was a surprise to me
> to find out that in the background they were present anyway.
>
> 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