Avoiding global variables

Global variables can be accessed for reading or writing in any scope of an application. They are a necessary evil. Indeed, any application needs to organize its code structure in order to process input values and return the appropriate response or output. Problems and bugs start occurring when the code is not well organized and when any part of the code can therefore modify the global state of the rest of the application and modify the program's overall expected behavior.

Firstly, poorly organized code means that the scripting engine, or interpreter, has more work to do when trying to look up a variable name, because it will have to go through many scopes until it finds it in the global scope.

Secondly, poorly organized ...

Get Mastering The Faster Web with PHP, MySQL, and JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.