this might be a stupid question but I'm stuck here at this silly problem for the part 2 hours.
I have this function which checks if a particular config file's variable are 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 inc.config.php file:
$_GLOBAL=array();
$_GLOBAL['id'] = "asas";
$_GLOBAL['username'] = "asas";
$_GLOBAL['password'] = 'as';
Function Calling
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 the Please enter correct details. Even if my $_GLOBAL['username']='';
. What wrong am I doing here?
The problem is that $_GLOBAL
appears to be a PHP superglobal, but it is not -- there is no such superglobal in PHP. As a result, this variable is not immediately accessible 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 meant to use $GLOBALS
instead, though I would strongly advise against it. Use a name like $SETTINGS
instead, and don't forget to use 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 superglobals; this prefix implies a superglobal, while your variable is not. This will create unnecessary confusion.
$GLOBALS is what you mean to use, though this is not a good practice.