PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

php.ini中怎么屏蔽所有错误

青灯夜游
青灯夜游 原创
2021-09-07 19:09:56 1765浏览

php.ini中屏蔽所有错误的方法:1、打开“php.ini”配置文件,在其中搜索“display_errors”项;2、将“display_errors”项的值设置为“off”即可关闭所有的php错误报告,进而屏蔽所有错误。

本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑

php.ini中屏蔽所有错误的方法:

打开 php.ini 配置文件,在其中搜索 display_errors,然后将 display_errors 的值设置为 Off 即可关闭所有的 PHP 错误报告。如下所示:

; 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

这种方法应该是最彻底的一种解决办法,因为它则是作用于所有的 PHP 文件。

扩展知识:屏蔽错误的其他方法

  • 使用错误控制运算符:@

PHP 支持使用错误控制运算符@。将其放置在一个 PHP 表达式之前,该表达式可能产生的任何错误信息都将被忽略掉。

如果用 set_error_handler() 设定了自定义的错误处理函数,这个错误处理函数仍然会被调用,而如果在出错语句前使用了@的话,错误处理函数将返回 0。

需要注意的是,@运算符只对表达式有效。简单来说就是,如果能从某处得到值,就能在它前面加上 @ 运算符。例如可以在变量、函数、include 调用、常量等等之前使用 @ 运算符,但不能把它放在函数或类的定义之前,也不能用于条件结构例如 if 和 foreach 等语句前。

@运算符对于可以导致程序终止的严重错误也是有效的,这意味着如果在某个不存在或者敲错了字母的函数调用前用了@来抑制错误信息,那么程序将没有任何提示的死在那里。

【示例】使用 @ 错误控制运算符屏蔽代码中的错误。

<?php
    $link = @mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db") or die(&#39;数据库连接失败!&#39;);
?>

运行结果如下:

数据库连接失败!

推荐学习:《PHP视频教程

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。