zoukankan      html  css  js  c++  java
  • PHP-错误处理

    错误处理

    PHP语言开发简单之处也体现在可以提供丰富的错误信息。

    错误显示

    修改 php.ini 配置文件, display_errors = On 即开启错误显示。

    错误类型

    语法错误

    违背了程序语言的规则错误,称之为语法错误。比如不以分号结束的语句,或函数写错时都会出现语法错误。语法错误PHP会在运行前检测出来。

    下面代码没有以分号结束,将报语法错误

    <?php
    echo 'houdunren'
    

    错误内容如下

    ( ! ) Parse error: syntax error, unexpected end of file, expecting ',' or ';' in C:wamp64wwwphpindex.php on line 3
    

    运行错误

    经过语法错误检测后,将开始运行PHP代码,在此发生的错误为运行时错误。

    以下代码因为加载不存在文件,所以会发生运行时错误。

    <?php
    require 'houdunren';
    

    错误内容如下

    ( ! ) Warning: require(houdunren): failed to open stream: No such file or directory in C:wamp64wwwphpindex.php on line 2
    

    常见运行错误如下:

    • 加载不存在文件
    • 连接数据库失败
    • 远程请求失败
    • 函数或类不存在

    如果有用户数据参与的脚本,需要对数据进行校验。

    逻辑错误

    逻辑错误是指软件开发工程师在业务逻辑开发中造成错误。

    下面展示一个工程师分析不到位,造成的逻辑错误示例。

    for ($i = 0; $i < 5; $i--) {
        echo $i;
    }
    

    错误报告

    常见错误类型

    常量 描述
    1 E_ERROR 致命的运行时错误。这类错误一般是不可恢复的情况,例如内存分配导致的问题。后果是导致脚本终止不再继续运行
    2 E_WARNING 运行时警告 (非致命错误)。仅给出提示信息,但是脚本不会终止运行。
    8 E_NOTICE 运行时通知。表示脚本遇到可能会表现为错误的情况。
    64 E_COMPILE_ERROR 致命编译时错误。类似 E_ERROR
    2048 E_STRICT 启用 PHP 对代码的修改建议,以确保代码具有最佳的互操作性和向前兼容性。
    8192 E_DEPRECATED 运行时通知。启用后将会对在未来版本中可能无法正常工作的代码给出警告。
    8191 E_ALL 所有错误和警告,除级别 E_STRICT 以外。

    关闭警告与致命错误

    error_reporting(~E_WARNING & ~E_COMPILE_ERROR);
    require('a');
    

    显示除通知外的所有错误

    error_reporting(E_ALL & ~E_NOTICE);
    echo $houdunren;
    

    关闭错误显示

    error_reporting(0);
    

    处理引擎

    通过自定义错误与日志处理引擎,让错误处理个性化。同时有些错误是在用户访问发生的,通过日志可以更全面检测网站运行状态。

    <?php
    namespace Core;
    class Error
    {
    	# debug 开启状态
        protected $debug;
        public function __construct($debug = true)
        {
            $this->debug = $debug;
        }
        public function bootstrap()
        {
            error_reporting(0);
            # 自定义错误处理
            set_error_handler([$this, 'error'], E_ALL | E_STRICT);
        }
        public function error($errno, $error, $file, $line)
        {
            $msg = $error . "($errno)" . $file . " ($line).";
            switch ($errno) {
                case E_USER_ERROR:
                case E_USER_NOTICE:
                    break;
                case E_NOTICE:
                	# 开启debug时显示提示消息
                    if ($this->debug)
                        echo $msg;
                    break;
                default:
                    if ($this->debug) {
                        include 'debug.php';
                    } else {
                        $file =  'logs/' . date('Y_m_d') . '.log';
                        # 记录错误日志到文件
                        error_log(date("[ c ]") . $msg . PHP_EOL, 3, $file, null);
                    }
                    die;
            }
        }
    }
    (new Error(false))->bootstrap();
    include('houdunren.com');
    

    错误显示页面

    debug.php用于显示开启debug时的错误显示页面。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
        <div class="container mt-3">
            <div class="card">
                <div class="card-header">
                    <?php echo 'File:' . $file . '  Line:' . $line; ?>
                </div>
                <div class="card-body p-0">
                    <ul class="list-group list-group-flush">
                        <?php foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $id => $f) : ?>
                            <li class="list-group-item">
                                <?php echo "#$id " . $f['file'] . "({$f['line']})<br/>"; ?>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            </div>
        </div>
    </body>
    </html>
    
  • 相关阅读:
    win7下的vxworks总结
    ubuntu 无法获得锁 /var/lib/dpkg/lock
    项目中用到了的一些批处理文件
    win7下安装 WINDRIVER.TORNADO.V2.2.FOR.ARM
    使用opencv统计视频库的总时长
    January 05th, 2018 Week 01st Friday
    January 04th, 2018 Week 01st Thursday
    January 03rd, 2018 Week 01st Wednesday
    January 02nd, 2018 Week 01st Tuesday
    January 01st, 2018 Week 01st Monday
  • 原文地址:https://www.cnblogs.com/pengcode/p/12585416.html
Copyright © 2011-2022 走看看