Re: Re: Local variable protection

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 14 Oct 2011 at 16:46, Tedd Sperling <tedd.sperling@xxxxxxxxx> wrote: 

> On Oct 13, 2011, at 11:37 AM, Tim Streater wrote:
>> On 13 Oct 2011 at 16:25, Tedd Sperling <tedd.sperling@xxxxxxxxx> 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux