dqzlqfqeh845799833
2019-02-28 15:18
浏览 363
已采纳

使用未定义的常量PHP ver 7.2

It looks like there are quite some posts on this topic, however this case seems to be different.

In PHP version greater than or equal to 7.2 the following code:

if (!empty($_GET['rxdebug']) || AH_DEBUG){
    define(AH_DEBUG_MODE, true);
}else{
    define(AH_DEBUG_MODE, false);
}

Will throw an error on second define line define(AH_DEBUG_MODE, false);

Use of undefined constant AH_DEBUG_MODE- assumed 'AH_DEBUG_MODE' (this will throw an Error in a future version of PHP) in some-file.php on line xxx

Basically the first define is accepted, the second one no....Isn't the code processed one time only? Or does a sort of pre-processor run beforehand throwing the error as if the constant is defined twice?

图片转代码服务由CSDN问答提供 功能建议

看起来这个主题有很多帖子,但是这个案例似乎有所不同。

在大于或等于7.2的PHP版本中,使用以下代码:

  if(!empty($ _ GET ['rxdebug'])|| AH_DEBUG  ){
 define(AH_DEBUG_MODE,true); 
} else {
 define(AH_DEBUG_MODE,false); 
} 
   
 
 

将抛出错误 第二个定义行 define(AH_DEBUG_MODE,false);

 使用未定义的常量AH_DEBUG_MODE-假定'AH_DEBUG_MODE'(这将在将来引发错误 在xxx行上的some-file.php中的PHP文件版本  
 
 

基本上第一个定义被接受,第二个定义没有....不是代码 只处理过一次? 或者是否有一种预处理器预先运行抛出错误,好像常量被定义了两次?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dtp87205 2019-02-28 15:19
    已采纳

    Should be:

    define('AH_DEBUG_MODE', true);
    

    Having: define(AH_DEBUG_MODE, true) you force PHP to evaluate the value of a constant AH_DEBUG_MODE which is obviously not defined yet.

    Your code can be simplified to:

    define('AH_DEBUG_MODE', !empty($_GET['rxdebug']) || AH_DEBUG);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drb88830 2019-02-28 15:21

    Your code should be wrapping the constant's name in quotes. Check the manual at http://php.net/manual/en/function.define.php

    if (!empty($_GET['rxdebug']) || AH_DEBUG){
        define('AH_DEBUG_MODE', true);
    }else{
        define('AH_DEBUG_MODE', false);
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题