dongyu9667 2014-09-01 20:24
浏览 119
已采纳

配置文件中设置的$ _GLOBAL变量未正确读取

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?

  • 写回答

2条回答 默认 最新

  • dongluan7821 2014-09-01 20:31
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢