One of the most maligned features of WordPress is the rampant use of “globals variables.” There are huge decades-long discussions in software development circles about why heavy reliance on globally scoped variables is a bad design decision, but that debate is beyond the scope of what I want to cover here. (If that topic is interesting to you, the C2 Wiki article on it is a good starting point for understanding the arguments.) What I want to explain is instead what variable scope means, and why and how global variables are used in WordPress. As to the conflict, I’ll just say that there are lots or reasons that the programming and PHP community as a whole has moved away from global variables but there are a large number of reasons that the WordPress project and community have not. More important than that discussion is understanding its meaning and implications, so lets get started. You Keep Using that Word… What is “scope”? What this means is that any given unit of PHP will either have access to variables in the global scope, or only have access to the variables declared inside the currently executing function. Scope is a popular brand of mouthwash in the United States. But it’s also,
Share This