When you’re just starting to work with functions and objects in PHP, variable scope can be the cause of some confusion. Fortunately, PHP’s scope rules are relatively easy to understand (compared to some other languages, at least!).
In this tutorial you’ll learn all you need to know about variable scope in PHP. You’ll look at:
- The concept of variable scope — what it is, and what it means
- The differences between global and local scope
- How to access global variables from within a function
- PHP’s superglobals, and how they work
- How to use static variables to preserve state
Let’s get started!