The intent of global variables is to allow all of the code, all plugins and themes, to be able to both read and write to the variable. You want anything within the website’s PHP code to have full, unprotected access to the data that your variable represents. That means that any code within the website can change the data. For example,
$wp_query is a global variable. Therefore, any piece of code in WordPress Core, plugins, and theme can both read and change the data.
Tip from me to you
Globals can lead to unexpected behavior. Why? Because everyone has access to it and can change the data. Therefore, you only want to use globals when you want anyone to both read and write (change the data) for that variable. Otherwise, use a function and static variable. Then the data is protected, meaning anyone can read it, but no one can overwrite it.
Keep It Simple, Stupid (KISS) - the best kiss you'll get in code.
Total Lab Runtime: 01:50:52
- 1 PHP Variables Introductionfree 03:44
- 2 PHP Variables Intent and Namingfree 12:43
- 3 Variable Scopebasic 08:24
- 4 Function Parametersbasic 08:46
- 5 Variable - PHP Internalspro 11:08
- 6 Global Variablesbasic 09:10
- 7 Global Variables - PHP Internalspro 02:52
- 8 Static Variablesbasic 13:05
- 9 Static Variables - PHP Internalspro 05:01
- 10 Passing Variables By Referencepro 10:04
- 11 Passing Variables By Reference - PHP Internalspro 08:01
- 12 Namespacingbasic 01:37
- 13 Variable Scope When Including Filesbasic 08:24
- 14 Type Jugglingbasic 07:53