【问题标题】:bitwise operators and error levels (php)按位运算符和错误级别 (php)
【发布时间】:2011-01-26 01:21:08
【问题描述】:

我已经创建了一个用户错误处理程序,并且我想确保我正确使用了位运算符。

这是我的配置设置,用于设置以哪种方式处理哪些错误:

// user error logging level (change for production)
define('LEV_USER_ERROR_LOG_LEVEL', E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);

// user error display level (change for production)
define('LEV_USER_ERROR_DISPLAY_LEVEL', E_USER_ERROR);

这是我设置用户错误处理程序的方式:

// set user error handler
set_error_handler('user_error_handler', E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);

这是错误处理程序本身:

    // user error handler
    public static function user_error_handler($error_level, $message, $file_name, $line_number) {
        if (LEV_USER_ERROR_LOG_LEVEL | LEV_USER_ERROR_DISPLAY_LEVEL == 0) return true;
        switch ($error_level) {
            case E_USER_ERROR:
                if (LEV_USER_ERROR_LOG_LEVEL & E_USER_ERROR) {
                    error_log('[' . date('Y-m-d h:i:s') . '] User Error: "' . $message . '", File: "'.$file_name.'", Line: '.$line_number.', Request: "' . $_SERVER['ORIG_PATH_INFO'] . "\"\n", 3, 'application/logs/user_error_log.txt');
                }
                if (LEV_USER_ERROR_DISPLAY_LEVEL & E_USER_ERROR) {
                    echo '[' . date('Y-m-d h:i:s') . '] User Level Error: "' . $message . '", File: "'.$file_name.'", Line: '.$line_number.'<br />';
                }
                die;
                break;
            case E_USER_WARNING:
                if (LEV_USER_ERROR_LOG_LEVEL & E_USER_WARNING) {
                    error_log('[' . date('Y-m-d h:i:s') . '] User Warning: "' . $message . '", File: "'.$file_name.'", Line: '.$line_number.', Request: "' . $_SERVER['ORIG_PATH_INFO'] . "\"\n", 3, 'application/logs/user_error_log.txt');
                }
                if (LEV_USER_ERROR_DISPLAY_LEVEL & E_USER_WARNING) {
                    echo '[' . date('Y-m-d h:i:s') . '] User Level Warning: "' . $message . '", File: "'.$file_name.'", Line: '.$line_number.'<br />';
                }
                break;
            case E_USER_NOTICE:
                if (LEV_USER_ERROR_LOG_LEVEL & E_USER_NOTICE) {
                    error_log('[' . date('Y-m-d h:i:s') . '] User Notice: "' . $message . '", File: "'.$file_name.'", Line: '.$line_number.', Request: "' . $_SERVER['ORIG_PATH_INFO'] . "\"\n", 3, 'application/logs/user_error_log.txt');
                }
                if (LEV_USER_ERROR_DISPLAY_LEVEL & E_USER_NOTICE) {
                    echo '[' . date('Y-m-d h:i:s') . '] User Level Notice: "' . $message . '", File: "'.$file_name.'", Line: '.$line_number.'<br />';
                }
                break;
            default:
                // call PHP internal error handler
                return false;
        }
        // do not call PHP internal error handler
        return true;
    }

问题:

  1. 在我的处理程序设置中使用的按位或运算符将使错误处理程序仅在发生这三个错误之一时才被调用?
  2. 在我的错误处理程序的第一行中使用的按位 OR 运算符将使它只有当两个配置设置都设置为零时,函数才会退出?

【问题讨论】:

    标签: php bitwise-operators error-reporting


    【解决方案1】:

    排队

    if (LEV_USER_ERROR_LOG_LEVEL | LEV_USER_ERROR_DISPLAY_LEVEL == 0) return true;
    

    比较比位运算符强,所以需要括号:

    if ((LEV_USER_ERROR_LOG_LEVEL | LEV_USER_ERROR_DISPLAY_LEVEL) == 0) return true;
    

    然后您的代码将按照您的描述工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      相关资源
      最近更新 更多