GVKun编程网logo

详解 PHP error_reporting(详解中美元首通话细节)

16

本文将带您了解关于详解PHPerror_reporting的新内容,同时我们还将为您解释详解中美元首通话细节的相关知识,另外,我们还将为您提供关于crystalreportPHP中error_repo

本文将带您了解关于详解 PHP error_reporting的新内容,同时我们还将为您解释详解中美元首通话细节的相关知识,另外,我们还将为您提供关于crystal report PHP中error_reporting函数的用法修改PHP屏蔽错误、error_reporting and find error in php program、error_reporting() 设置 PHP 的报错级、error_reporting()函数用法,errorreporting的实用信息。

本文目录一览:

详解 PHP error_reporting(详解中美元首通话细节)

详解 PHP error_reporting(详解中美元首通话细节)

   经常在 php.ini 里看到 php 输出的错误级别没有深究,今天终于细致的查看了一下,分享出来。在自己开发项目时经常要查看错误信息,这就需要打开错误提示,在 php.ini 里打开设置 display_errors = On, 但项目一旦上线在线上环境中就不要这样做了,防止黑客拿到服务器的错误提示了解项目细节。

    当然除了在 php.ini 里设置外还可以在项目文件里设置 php 错误提示的级别。

ini_set(''display_error''''On'');
error_reporting(E_ALL ^ E_NOTICE);

ini_set () 函数临时修改 php.ini 的设置,error_reporting 设置 php 显示错误的级别。error_reporting 里参数的内容我们需要深究一下。E_ALL、E_ERROR、E_NOTICE 是 php 预定义的常量,大家可以打印出来看看数值,当然这些十进制的数值要转成二进制才能看到其中的奥秘。

预定义常量 十进制 二进制
E_ALL 30719 111011111111111
E_ERROR 1 1
E_WARNING 2 10
E_NOTICE 8 100
E_CORE_ERROR 16 1000

大家也可以打印出来查看一下:

print_r(get_defined_constants());

转成二进制就可以进行位操作了,''&'' 按位与,''|'' 按位或,''~'' 取反,''^'' 异或,这样就可以解释:

error_reporting(E_ALL ^ E_NOTICE);
error_reporting(E_ALL & ~E_NOTICE);

上面两句都表示:除了运行时的通知不显示之外所有的提示都打印出来。

crystal report PHP中error_reporting函数的用法修改PHP屏蔽错误

crystal report PHP中error_reporting函数的用法修改PHP屏蔽错误

今天学习CI框架过程中遇到个问题:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: user
一般在默认的普通PHP文件中输出一个未定义声明的变量是不会报错误的,但在codeigniter框架下却要报错误,这对于想集成 添加 和 修改 页面于一体的”懒人”很不方便,由于是初学者开始还想怎么在代码中屏蔽这一错误提示呢.甚至用到了@,但听很多人都说@会大大降低性能….
最后突然想到,是不是codeigniter有意让这错误信息提示出来了呢,我们该如何去屏蔽掉这一类错误呢无意中搜索到了”如何让codeigniter不显示Notice信息?”,茅塞顿开.原来是入口index.php中的error_reporting(E_ALL);在作怪.只需要把它改成
  error_reporting(E_ALL ^ E_NOTICE);
就可以屏蔽掉这个错误,而不影响其他的报错.
下边是搜索到的一些资料:
error_reporting() 设置 PHP 的报错级别并返回当前级别。
语法
error_reporting(report_level)
如果参数 level 未指定,当前报错级别将被返回。下面几项是 level 可能的值:
1 E_ERROR
2 E_WARNING
4 E_PARSE
8 E_NOTICE
16 E_CORE_ERROR
32 E_CORE_WARNING
64 E_COMPILE_ERROR
128 E_COMPILE_WARNING
256 E_USER_ERROR
512 E_USER_WARNING
1024 E_USER_NOTICE
2047 E_ALL
2048 E_STRICT
E_NOTICE 表示一般情形不记录,只有程式有错误情形时才用到,例如企图存取一个不存在的变数,或是呼叫 stat() 函式检视不存在的档案。
E_WARNING 通常都会显示出来,但不会中断程式的执行。这对除错很有效。例如:用有问题的常规表示法呼叫 ereg()。
E_ERROR 通常会显示出来,亦会中断程式执行。意即用这个遮罩无法追查到记忆体配置或其它的错误。
E_PARSE 从语法中剖析错误。
E_CORE_ERROR 类似 E_ERROR,但不包括 PHP 核心造成的错误。
E_CORE_WARNING 类似 E_WARNING,但不包括 PHP 核心错误警告。
PHP 的错误报告
  php.ini 文件中有许多配置设置。您应当已经设置好自己的 php.ini 文件并把它放在合适的目录中,就像在 Linux 上安装 PHP 和 Apache 2 的文档说明中所示的那样。在调试 PHP 应用程序时,应当知道两个配置变量。下面是这两个变量及其默认值:
    display_errors = Off
    error_reporting = E_ALL
  通过在 php.ini 文件中搜索它们,可以发现这两个变量当前的默认值。display_errors 变量的目的很明显 —— 它告诉 PHP 是否显示错误。默认值是 Off。但是,要让开发过程更加轻松,请把这个值设为 On:
    display_errors = On
  error_reporting 变量的默认值是 E_ALL。这个设置会显示从不良编码实践到无害提示到出错的所有信息。E_ALL 对于开发过程来说有点太细,因为它在屏幕上为一些小事(例如变量未初始化)也显示提示,会搞糟浏览器的输出。我只想看到错误和不良编码实践,但是不想看到无害的提示。所以,请用以下值代替 error_reporting 的默认值:
    error_reporting = E_ALL & ~E_NOTICE
  重新启动 Apache,就全部设置好了。接下来,将学习如何在 Apache 上做同样的事。
  服务器上的错误报告
  依赖于 Apache 正在做的工作,在 PHP 中打开错误报告可能没法工作,因为在计算机上可能有多个 PHP 版本。有时很难区分 Apache 正在使用哪个 PHP 版本,因为 Apache 只能查看一个 php.ini 文件。不知道 Apache 正在使用哪个 php.ini 文件配置自己是一个安全问题。但是,有一种方法可以在 Apache 中配置 PHP 变量,从而保证设置了正确的出错级别。
  而且,最好知道如何在服务器端设置这些配置变量,以否决或抢占 php.ini 文件,从而提供更高级别的安全性。
在配置 Apache 时,应该已经接触过 /conf/httpd.conf 中 http.conf 文件中的基本配置。
  要做在php.ini文件中已经做过的事,请把下列各行添加到 httpd.conf,覆盖任何 php.ini 文件:
    php_flag display_errors on
    php_value error_reporting 2039
  这会覆盖在 php.ini 文件中为 display_errors 已经设置的标志,以及 error_reporting 的值。值 2039 代表 E_ALL & ~E_NOTICE。如果愿意采用 E_ALL,请把值设为 2047。同样,还是要重启 Apache。
  接下来,要在服务器上测试错误报告。
关于error_reporting()这个函数,它是可以屏蔽到一些错误信息,但是PHP 核心造成的错误,是无法屏蔽的,因为PHP 核心造成的错误会直接导致PHP文件编译失败,因为书写格式没有按照PHP的编码规则写而造成的错误,是无法屏蔽的

复制代码 代码如下:


* For now, avoid warnings of E_STRICT mode
* (this must be done before function definitions)
*/
if (defined(''E_STRICT'')) {
$old_error_reporting = error_reporting(0);
if ($old_error_reporting & E_STRICT) {
error_reporting($old_error_reporting ^ E_STRICT);
} else {
error_reporting($old_error_reporting);
}
unset($old_error_reporting);


常见的如下:

复制代码 代码如下:


// Turn off all error reporting;关闭所有的错误
error_reporting(0);
// Report simple running errors;报告一个简单的运行错误
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings …);包括报告一些未初始化的变量或捕捉变量名的拼写错误
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini;报告所有的错误但不包括E_NOTICE 这也是php.ini的缺省设置
error_reporting(E_ALL ^ E_NOTICE);
// Report all PHP errors (bitwise 63 may be used in PHP 3);报告所有的错误
error_reporting(E_ALL);
// Same as error_reporting(E_ALL);同上
ini_set(''error_reporting'', E_ALL);

以上就介绍了crystal report PHP中error_reporting函数的用法修改PHP屏蔽错误,包括了crystal report方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

error_reporting and find error in php program

error_reporting and find error in php program

代码

<p> <? php /* 开启php.ini中的display_errors指令,只有该指令开启如果有错误报告才能输出 */ ini_set ( '' display_errors '' , 1 ); /* 通过error_reporting()函数设置在本脚本中,输出所有级别的错误报告 */ error_reporting ( E_ALL ); /* “注意(notice)”的报告,不会阻止脚本的执行,并且可能不一定是一个问题 */ getType ( $var ); // 调用函数时提供的参数变量没有在之前声明 /* “警告(warning)”的报告,指示一个问题,但是不会阻止脚本的执行 */ getType (); // 调用函数时没有提供必要的参数 /* “错误(error)”的报告,它会终止程序,脚本不会再向下执行 */ get_Type(); // 调用一个没有被定义的函数 ?> </p>
登录后复制

Description: 1.error_reporting () to set PHP''s error-level and return to the current level. Its scope is the current page

2.Display_errors directive in php.ini open, only the instruction to open the output if there is an error report

立即学习“PHP免费学习笔记(深入)”;

立即学习“PHP免费学习笔记(深入)”;

立即学习“PHP免费学习笔记(深入)”;

语法

error_reporting(report_level)
登录后复制

If the parameter level is not specified, the current error level will be returned. The following is the level of several possible values:

值 常量 描述
1 E_ERROR Fatal run-time errors. Errors that can not be recovered from. Execution of the script is halted
2 E_WARNING Non-fatal run-time errors. Execution of the script is not halted
4 E_PARSE Compile-time parse errors. Parse errors should only be generated by the parser
8 E_NOTICE Run-time notices. The script found something that might be an error, but could also happen when running a script normally
16 E_CORE_ERROR Fatal errors at PHP startup. This is like an E_ERROR in the PHP core
32 E_CORE_WARNING Non-fatal errors at PHP startup. This is like an E_WARNING in the PHP core
64 E_COMPILE_ERROR Fatal compile-time errors. This is like an E_ERROR generated by the Zend Scripting Engine
128 E_COMPILE_WARNING Non-fatal compile-time errors. This is like an E_WARNING generated by the Zend Scripting Engine
256 E_USER_ERROR Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error()
512 E_USER_WARNING Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error()
1024 E_USER_NOTICE User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error()
2048 E_STRICT Run-time notices. PHP suggest changes to your code to help interoperability and compatibility of the code
4096 E_RECOVERABLE_ERROR Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler())
8191 E_ALL All errors and warnings, except level E_STRICT (E_STRICT will be part of E_ALL as of PHP 6.0)

Examples

Any number of the above options can be used "or"to connect (with OR or |), so that all required to report the level of error. For example, the following code to turn off the user-defined errors and warnings, the implementation of certain operations, and then return to the original error level:

<p> <prename="code"> <p> <? php // 禁用错误报告 error_reporting ( 0 ); // 报告运行时错误 error_reporting ( E_ERROR | E_WARNING | E_PARSE ); // 报告所有错误 error_reporting ( E_ALL ); ?> </p>
登录后复制

error_reporting() 设置 PHP 的报错级

error_reporting() 设置 PHP 的报错级

error_reporting(e_all ^ e_notice ^ e_strict ^ e_deprecated);
哥们们,帮我解释下这行代码是什么意识,设置什么报错级别,

回复内容:

error_reporting(e_all ^ e_notice ^ e_strict ^ e_deprecated);
哥们们,帮我解释下这行代码是什么意识,设置什么报错级别,

查阅PHP官方文档就可以知道:error_reporting
上面的代码表示的意思是:除了E_NOTICE E_SCRICT E_DEPRECATED 之外的错误全部都报告。
E_ALL ^ E_NOTICE ^ E_STRICT ^ E_DEPRECATED这个是连续的异或操作。
E_ALL E_NOTICE E_STRICT E_DEPRECATED 这些都是常量,对应的二进制大致如下

E_NOTICE 00001 
E_STRICT 00010
E_DEPRECATED 00100
E_ALL 11111
登录后复制

进行异或操作之后,相当于是把相同的变成0 不同的变成1 也就是说得到的结果是E_ALL排除了进行异或操作的那几个。
以上常量对应的值纯属虚构,具体可以试着打印出来看下到底是多少。
同理还可以使用 E_NOTICE | E_STRICT这样的方法来设置多个级别进行报告。原理是进行或操作之后有1则1,全0则0

这里所代表是意思是:输出所有类型的错误(E_ALL),但排除E_NOTICE、E_STRICT、E_DEPRECATED这几种类型的错误。

立即学习“PHP免费学习笔记(深入)”;

点击下载“修复打印机驱动工具”;

这里使用了一个位运算的技巧,因为E_ALL是一个全1数,而E_NOTICE、E_STRICT、E_DEPRECATED都为单1数,这几个异或一下,就形成了一个排除这几项的日志级别的数。

error_reporting()函数用法,errorreporting

error_reporting()函数用法,errorreporting

error_reporting()函数用法,errorreporting

首先要知道error_reporting()函数是用来设置错误级别并返回当前级别的。它有14个错误级别,如下:

1        E_ERROR          致命的运行时错误。 错误无法恢复过来。脚本的执行被暂停
2        E_WARNING        非致命的运行时错误。 脚本的执行不会停止
4        E_PARSE          编译时解析错误。解析错误应该只由分析器生成
8        E_NOTICE         运行时间的通知。
16       E_CORE_ERROR     在PHP启动时的致命错误。这就好比一个在PHP核心的E_ERROR
32       E_CORE_WARNING   在PHP启动时的非致命的错误。这就好比一个在PHP核心E_WARNING警告
64       E_COMPILE_ERROR  致命的编译时错误。 这就像由Zend脚本引擎生成了一个E_ERROR
128      E_COMPILE_WARNING 非致命的编译时错误,由Zend脚本引擎生成了一个E_WARNING警告
256      E_USER_ERROR     致命的用户生成的错误。
512      E_USER_WARNING   非致命的用户生成的警告。 
1024     E_USER_NOTICE    用户生成的通知。
2048     E_STRICT         运行时间的通知。

4096     E_RECOVERABLE_ERROR 捕捉致命的错误。

8191     E_ALL来         所有的错误和警告。

好像php默认是不开启错误的,所以你需要配置php.ini文件:

将 display_errors = Off 改为display_errors = On 

另外还要配置错误级别:将

error_reporting = E_ALL     改为:

error_reporting = E_ALL & ~E_NOTICE

应为php默认是显示所有错误的,而有些无害的提示我们不需要显示,所以设置如上!

也可以在php代码运用如下:

 

    1. php
    2. //禁用错误报告,也就是不显示错误
    3. error_reporting(0);
    4. //报告运行时错误
    5. error_reporting(E_ERROR | E_WARNING | E_PARSE);
    6. //报告所有错误
    7. error_reporting(E_ALL);
    8. ?>

关于详解 PHP error_reporting详解中美元首通话细节的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于crystal report PHP中error_reporting函数的用法修改PHP屏蔽错误、error_reporting and find error in php program、error_reporting() 设置 PHP 的报错级、error_reporting()函数用法,errorreporting等相关内容,可以在本站寻找。

本文标签: