注意使用isset和array_key_exists()时未定义的索引:文件

时间:2014-04-15 07:22:47

标签: php codeigniter notice

我在用CodeIgniter编程php时遇到了这个注意事项 像这样的代码。它用于在记录时显示行和文件名。 我试过这个

    //get debug messages such as functionname, linenum,etc.
    if ($level == 'debug') {
        $debug_info = debug_backtrace(!DEBUG_BACKTRACE_PROVIDE_OBJECT
                                && DEBUG_BACKTRACE_IGNORE_ARGS);
        $debug_info = $debug_info[1];
        $debug_message = '('.
            isset($debug_info['file'])?$debug_info['file']:''.
            isset($debug_info['class'])?$debug_info['class']:''.
            isset($debug_info['type'])?$debug_info['type']:''.
            isset($debug_info['function'])?$debug_info['function']:''.
            isset($debug_info['line'])?$debug_info['line']:''.
            ')';
        $message = $debug_message.$message;
    }

并使用array_key_exist()进行判断,但它仍然会导致NOTICE这样

  

遇到PHP错误

     

严重性:注意

     

消息:未定义索引:文件

     

文件名:core / Common.php

     

行号:364

非常感谢您回答这个问题

这是转储数据

array(4) {
  ["function"]=>
  string(5) "index"
  ["class"]=>
  string(7) "Welcome"
  ["type"]=>
  string(2) "->"
  ["args"]=>
  array(0) {
  }
}

1 个答案:

答案 0 :(得分:2)

'(' . isset(...)将始终评估为true,因此将始终评估$debug_info['file']是否设置。问题在于您将条件和操作链接起来的方式。您需要明确地对它们进行分隔和分组,否则它不会按照您的想法进行分组。

'(' . (isset(..) ? 'foo' : 'bar') . ( .. ? .. : .. ) . ...
相关问题