美文网首页PHP
PHP 开启或关闭错误提示

PHP 开启或关闭错误提示

作者: 887d1fc86fe6 | 来源:发表于2020-04-26 11:22 被阅读0次
  • PHP 默认不会在浏览器中显示或者报出错误信息,这里我们需要手动开启。

  • 如果有错误发生(触发了错误),默认情况下会被显示在页面(即输出的结果页面)。

  • 我们可以对此进行设置,以诀定以下两点:

    1、设置 display_errors 以决定是否显示错误:

    • 在php.ini中设置:
      display_errors = On 或 Off;// 这里设置,影响所有使用该php语言引擎的代码(网站页面) ;

    • 在php文件中设置:
      ini_set("display_errors", 1 或 0 ('On' 或 'Off'));// 1 (On) 表示显示,0 (Off) 表示不显示,在这里设置,只影响当前网页代码本身。

    2、设置 error_reporting 以决定显示哪些错误:

    • 在php.ini中设置:
      error_reporting = 错误代号1 | 错误代号 2 //;(要显示的就写出来,或者可以写E_ALL,表示显示所有)

    • 在php文件中设置:
      ini_set("error_reporting", 错误代号1 | 错误代号 2);
      或者
      error_reporting(错误代号1 | 错误代号 2); // 例如 error_reporting(E_ALL | E_STRICT);

    3、display_errors 与 error_reporting 两者都需要设置,可以从两者的设置方法里面选其一就好了,不过建议要么 php.ini 文件修改,要么就代码里面添加。


  • 方式一:在PHP文件最顶部加入开启错误提示代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <?php
  // 在PHP文件最顶部加入开启错误提示代码
  ini_set("display_errors", "On"); 
  error_reporting(E_ALL | E_STRICT);

  // 错误的导入文件以及输出未定义的对象
  include 'lib/nav1.html';
  echo '<br>当前的页码为:' . $page;
  ?>
</body>
</html>
  • 方式二:修改 php.ini 配置,开启错误提示
# 开发错误提示
display_errors = Off 修改为 display_errors = On

# 修改错误级别
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
修改为
error_reporting = E_ALL

在 php.ini 文件中的位置:

; This directive controls whether or not and where PHP will output errors,
; notices and warnings too. Error output is very useful during development, but
; it could be very dangerous in production environments. Depending on the code
; which is triggering the error, sensitive information could potentially leak
; out of your application such as database usernames and passwords or worse.
; For production environments, we recommend logging errors rather than
; sending them to STDOUT.
; Possible Values:
;   Off = Do not display any errors
;   stderr = Display errors to STDERR (affects only CGI/CLI binaries!)
;   On or stdout = Display errors to STDOUT
; Default Value: On
; Development Value: On
; Production Value: Off
; http://php.net/display-errors
# 开发错误提示
display_errors = Off // 修改为 display_errors = On

; Common Values:
;   E_ALL (Show all errors, warnings and notices including coding standards.)
;   E_ALL & ~E_NOTICE  (Show all errors, except for notices)
;   E_ALL & ~E_NOTICE & ~E_STRICT  (Show all errors, except for notices and coding standards warnings.)
;   E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR  (Show only errors)
; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
; Development Value: E_ALL
; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
; http://php.net/error-reporting
# 修改错误级别
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
// 修改为
error_reporting = E_ALL

相关文章

  • PHP 开启或关闭错误提示

    PHP 默认不会在浏览器中显示或者报出错误信息,这里我们需要手动开启。 如果有错误发生(触发了错误),默认情况下会...

  • PHP 错误处理、自定义错误、错误编号

    PHP 开启或关闭错误提示 错误分类语法错误:程序不能运行,是在运行之前,检查语法的时候,就发现语法出错.结果是提...

  • php配置记录

    针对一些php常用的一些配置: 1.关闭开启错误提示: ini_set("display_errors","On"...

  • PHP异常、错误处理机制笔记

    本文介绍PHP的异常,错误以及如何屏蔽错误。参考:php异常、错误处理机制、PHP错误提示的关闭方法详解 首先要明...

  • PHP开启错误提示

    下面提供两种方法 1. 修改php.ini文件 2. 在php文件开头调用以下函数

  • PHP开启或关闭错误报告

    关闭错误报告 在PHP页首加上 error_reporting(3); 0表示不显示 1只显示错误 2显示错误和警...

  • mac的php环境

    关闭apache随系统启动 开启apache 重启apache: 关闭apache: 开启PHP开启PHP,需要修...

  • PHP中的异常处理

    一下内容纯属于个人笔记 PHP中的异常处理(在没有关闭php.ini中的错误提示下) 在PHP中异常和错误是不一样...

  • PHP.INI安全配置

    参考PHP.INI安全配置 在网站上线之前记得在php.ini文件中设置关闭错误提示在php.ini文件的disp...

  • linux下php开启错误提示

    1. 配置php.ini打开php.ini,把display_errors = Off 改成 display_er...

网友评论

    本文标题:PHP 开启或关闭错误提示

    本文链接:https://www.haomeiwen.com/subject/qxddwhtx.html