Monday 27 March 2017

Re: [dcphp-dev] Global Variables Question

Whitney,

This advice is likely because include/require can be placed inside of a function (as well as in the global scope). When placed in a function, the included file's code executes within the scope of the function, so the included file's "global scope" is not *actually* in the global scope. (Think of include and require as being replaced with the content of the script they include. They do not create a new scope, nor drop back to the global scope for included code.

As a result, code written "in the global scope" may not actually be executing in the global scope, and so accessing an (unkeyworded) "global" variable may instead just be accessing a variable in that function's local scope. (You can make that determination at run-time, but it's more trouble than just throwing a global keyword in front of your globals and not worrying about it.)

With this in mind, it would make sense to declare all global variables as such when in the global scope, even if it's technically unnecessary. Although the use of global variables should be generally be minimized, sticking the global keyword in front of the globals in the global scope prevents one class of bugs (accessing a variable that you think is global but isn't actually), and at least gives you something to search for later.

-John


> On Mar 26, 2017, at 10:08, Whitney Yiu <wyiu416@gmail.com> wrote:
>
> Hi all,
>
> I am reading a book (PHP and MySQL Web Development - Thomson, Welling; 5th Ed.; Addison-Wesley) and am trying to understand the following passage regarding global variables:
>
> You can also use the global keyword at the top of a script when a variable is first used to declare that it should be in scope throughout the script. This is possibly a more common use of the global keyword.
>
> Can someone explain a use case for using a global variable at the top of a script? I thought that if a variable is declared outside of a function, then it is accessible throughout the script (except inside functions). I think I am missing something here.
>
> Thanks,
> Whitney
>
> --
> You received this message because you are subscribed to the Google
> Group: "Washington, DC PHP Developers Group" - http://www.dcphp.net
> To post, send email to washington-dcphp-group@googlegroups.com
> To unsubscribe, send email to washington-dcphp-group+unsubscribe@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/washington-dcphp-group?hl=en
> ---
> You received this message because you are subscribed to the Google Groups "Washington, DC PHP Developers Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to washington-dcphp-group+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google
Group: "Washington, DC PHP Developers Group" - http://www.dcphp.net
To post, send email to washington-dcphp-group@googlegroups.com
To unsubscribe, send email to washington-dcphp-group+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/washington-dcphp-group?hl=en
---
You received this message because you are subscribed to the Google Groups "Washington, DC PHP Developers Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to washington-dcphp-group+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

0 comments:

Post a Comment