As we know, global keyword makes variable( or object, array) visible inside current function we are dealing with
<?php
$some_var = 'some string';
function __test(){
global $some_var;
echo $some_var; // some string
}
But some dev’s still use the global keyword outside the functions
at first look it doesn’t make any sense to me.
Well, the question is: Does it make any sense to use ‘global’ keyword outside the function ???
From the docs:
Essentially you could have your function guts in a different file than your function declaration. These guts would then be included into the function. This would give the impression, if you view the guts alone, of a user using
globaloutside of a function, however the fact is that when this code is interpreted, it will be interpreted from within a function.Where the contents of our
functionGuts.phpfile could be:When viewed on its own,
functionGuts.phpwill give the impression thatglobalis being used outside of a function, when in reality it’s being used like this: