Scope and the global Statement

As discussed in Chapter 2, variables inside a function exist inside a name space separate from the global name space. Variables inside a function are private property and may never be seen or manipulated outside the function. However, there are two ways a function may access variables in the global scope: the global statement and the GLOBALS array.

The global statement brings a variable into a function's name space. Thereafter the variable may be used as if it were outside the function. Any changes to the variable will persist after execution of the function ceases. In the same way, it is possible to refer to global variables through the array GLOBALS. The array is indexed by variable names, so if you create a variable ...

Get Core PHP Programming: Using PHP to Build Dynamic Web Sites 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.