PHP variables and functions can be used anywhere in your code, regardless of the scope. Global variables and functions are accessible from any file in your project.
To declare a global variable, use the global keyword before the variable name.
global $name;
To declare a global function, use the global keyword before the function name.
global function myFunction() {
// function code
}
You can also access global variables and functions from within a function.
function myFunction() {
global $name;
echo $name;
}
You can also access global variables and functions from within a class.
class myClass {
global $name;
echo $name;
}
When you declare a global variable or function, it is available in all files in your project.