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

发布于 2024-10-14 06:55:35 字数 3015 浏览 5 评论 0原文

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

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

// 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. 我的处理程序设置中使用的按位 OR 运算符将使仅当这三个错误之一发生时才调用错误处理程序?
  2. 在我的错误处理程序的第一行中使用的按位或运算符将使得只有当两个配置设置都设置为零时,该函数才会退出?

I have created a user error handler, and I want to make sure that I am using bitwise operators correctly.

Here are my config settings to set which errors will be handled in which way:

// 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);

Here is how I set the user error handler:

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

Here is the error handler itself:

    // 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;
    }

Questions:

  1. The bitwise OR operator used in my handler setting will make it so that the error handler is ONLY called when one of those three errors occurs?
  2. The bitwise OR operator used in the first line of my error handler will make it so that ONLY if both of the config settings are set to zero, the function will exit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧话新听 2024-10-21 06:55:35

在该行中,

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;

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

In the line

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

the comparison is stronger than the bitwise operator, so you need parentheses:

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

Then your code will work as you describe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文