I used to think that all variables in PHP went through functions as normal. It wasn’t until I actually needed to read a global variable from a function that I realised it only has it’s own local variables. Having said that, if you need to use variables from outside of a function it is easy enough to parse through global variables.
The Solution
You have to tell PHP to read the global variable into the function. To do this simply type “global $myglobalvariable”, for example…
$myvar = "This is my global variable";
function myfunction() {
global $myvar; // This will bring the variable into the function.
$myvar .= " inside a function";
return $myvar;
}
This will return “This is my global variable inside a function”.
Again, really easy! Enjoy.




