Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
位运算符和错误级别(php)_Php_Bitwise Operators_Error Reporting - Fatal编程技术网

位运算符和错误级别(php)

位运算符和错误级别(php),php,bitwise-operators,error-reporting,Php,Bitwise Operators,Error Reporting,我已经创建了一个用户错误处理程序,我想确保正确使用了位运算符 以下是我的配置设置,用于设置将以何种方式处理哪些错误: // 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) def

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

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

// 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;
    }
//用户错误处理程序
公共静态函数用户错误处理程序($error\u level、$message、$file\u name、$line\u number){
如果(LEV_USER_ERROR_LOG_LEVEL | LEV_USER_ERROR_DISPLAY_LEVEL==0)返回true;
开关($error\u level){
案例E_用户_错误:
if(级别用户错误日志级别和E用户错误){
错误日志('['.date('Y-m-dh:i:s')。]用户错误:'.$message'.'',文件:'.$File\u name'.'',行:'.$Line\u number'.,请求:'.$u服务器['ORIG\u路径\u信息'.\''\n',3',应用程序/logs/User\u error\u log.txt');
}
if(级别用户错误显示级别和E用户错误){
回显“[”.date('Y-m-d h:i:s')。]用户级错误:“..message.”,文件:“..File_name.”,行:“..Line_number.”
; } 死亡 打破 案例E_用户_警告: if(级别用户错误日志级别和E用户警告){ 错误日志('['.date('Y-m-d h:i:s')。]用户警告:“..message.”,文件:“..File\U name.”,行:“..Line\U number.”,请求:“..U服务器['ORIG\U路径\U信息']”,\“\n”,3,'application/logs/User\U error\U log.txt'); } 如果(级别用户错误显示级别和E用户警告){ 回显“[”.date('Y-m-d h:i:s')。]用户级警告:“.$message.”,文件:“.$File_name.”,行:“.$Line_number.”
; } 打破 案例E_用户通知: if(级别用户错误日志级别和E用户通知){ 错误日志('['.date('Y-m-d h:i:s')。]用户通知:“.$message.”,文件:“.$File\u name.”,行:“.$Line\u number.”,请求:“.$u服务器['ORIG\u路径\u信息']”,“\'\n”,3,'application/logs/User\u error\u log.txt'); } 如果(级别用户错误显示级别和E用户通知){ 回显“[”.日期('Y-m-d h:i:s')。]用户级通知:“.$message.”,文件:“.$File_name.”,行:“.$Line_number.”
; } 打破 违约: //调用PHP内部错误处理程序 返回false; } //不要调用PHP内部错误处理程序 返回true; }
问题:

  • “我的处理程序”设置中使用的按位OR运算符将使其仅在发生这三个错误之一时调用错误处理程序
  • 我的错误处理程序的第一行中使用的按位OR运算符将使其成为仅当两个配置设置都设置为零时,函数才会退出
  • 排队

    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;
    
    然后,您的代码将按照您描述的那样工作