Re: Local variable protection
| From: | Tedd Sperling | Date: | Fri, 14 Oct 2011 15:46:03 +0000 |
| Subject: | Re: Local variable protection | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-315299@lists.php.net to get a copy of this message | ||
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.
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).
Cheers,
tedd
_____________________
tedd@sperling.com
http://sperling.com