$ _GLOBAL variables set in config file are not being read correctly
It might be a stupid question, but I was stuck here on this stupid issue for 2 hours.
I have this function that checks if a specific variable in a config file is not empty. Here's the function: -
include 'inc.config.php';
function not_valid_settings()
{
if((empty($_GLOBAL['id'])) || (empty($_GLOBAL['username'])) || empty($_GLOBAL['password']))
{
return true;
}
else
{
return false;
}
}
Here's the config file inc.config.php:
$_GLOBAL=array();
$_GLOBAL['id'] = "asas";
$_GLOBAL['username'] = "asas";
$_GLOBAL['password'] = 'as';
Function call
include 'inc/inc.functions.php';
if(not_valid_settings())
{
echo "Please enter correct entries in inc/inc.config.php";
exit();
}
For some reason I always get Please enter correct details. Even if mine $_GLOBAL['username']='';
. What am I doing here?
source to share
The problem is what $_GLOBAL
appears to be a superglobal PHP, but it is not - there is no such superglobal in PHP. As a result, this variable is not always available everywhere. If you add global $_GLOBAL;
as the first line if your function, your code should work:
function not_valid_settings()
{
global $_GLOBAL;
Perhaps you wanted to use $GLOBALS
this instead, although I would strongly advise it. Use the type name instead, $SETTINGS
and remember to use it global $SETTINGS;
in functions where you need to access the settings object.
In general, you should avoid choosing variable names that start with $_
unless they are PHP superglobal; this prefix implies superglobal, while your variable does not. This will create unnecessary confusion.
source to share