There will come a time in your experience in PHP that will require the use of a variable that remains the same for all functions. This variable should be called global and is best placed at the top of your PHP page in your variable declarations section. So, for example:
global $sitetitle;
$sitetitle = “Blazek Web Design”;
Now the problems lies in the fact that a function cannot see or even know of the global variables existence. To me, this defeats the whole purpose of the concept of global variables, but…whatever… There are a couple different work-arounds to make these global variables accessible to your functions. The first great to use if you only need to “see” a couple variables.
function somefunction() {
global $sitetitle;
echo $sitetitle; //OUTPUTS ‘Blazek Web Design’
}
So as you see above, re-declaring the global variable above will allow your function to use the variable as needed. This is sufficient for now, but if you have a lot of global variables, it does not make sense to list the entire set of global variables in every function. To make ALL global variables available in a function, do this:
function somefunction() {
extract($GLOBALS);
echo $sitetitle; //OUTPUTS ‘Blazek Web Design’
}
You need to know that each function does not inherently have access to your global variables, so these declarations need to be made in every function.







