PHP学习笔记 18 - 错误处理

  • PHP默认的错误处理方式:错误描述+出错文件+出错行号:

    Parse error: syntax error, unexpected '=' in F:\httpd-2.4.29-Win64-VC15\Apache24\htdocs\phpstudy\18error\error.php on line 2
  • 使用 die() 输出异常:

    <?php
    $f = fopen("123.txt", 'r') or die("Can not open file!");
    ?>

    输出:

    Warning: fopen(123.txt): failed to open stream: No such file or directory in F:\httpd-2.4.29-Win64-VC15\Apache24\htdocs\phpstudy\18error\die.php on line 2
    Can not open file!
  • PHP 允许用户设置错误处理器回调,错误处理器有5个参数(前2个必须):error_level, error_message, error_file, error_line, error_context

    <?php
    // 自定义错误
    function errorHandler($error_level, $error_msg) {
       echo "line: $error_level, error: $error_msg";
    }
    // 设置错误处理器
    set_error_handler("errorHandler");
    // 引发错误
    $f = fopen("123.txt", 'r');
    ?>

    输出:

    line: 2, error: fopen(123.txt): failed to open stream: No such file or directory
  • 引发错误:trigger_error()

    <?php
    trigger_error("Error here!");
    ?>

    输出:

    
    Notice: Error here! in F:\httpd-2.4.29-Win64-VC15\Apache24\htdocs\phpstudy\18error\trigger.php on line 2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值