<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Error Handling and Logging Functions</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="函数参考" HREF="funcref.html"><LINK REL="PREVIOUS" TITLE="enchant_dict_suggest" HREF="function.enchant-dict-suggest.html"><LINK REL="NEXT" TITLE="debug_backtrace" HREF="function.debug-backtrace.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="reference" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.enchant-dict-suggest.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.debug-backtrace.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="reference" ><A NAME="ref.errorfunc" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >XXXIII. Error Handling and Logging Functions</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN46894" ></A ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="errorfunc.intro" >简介</A ></H1 ><P > These are functions dealing with error handling and logging. They allow you to define your own error handling rules, as well as modify the way the errors can be logged. This allows you to change and enhance error reporting to suit your needs. </P ><P > With the logging functions, you can send messages directly to other machines, to an email (or email to pager gateway!), to system logs, etc., so you can selectively log and monitor the most important parts of your applications and websites. </P ><P > The error reporting functions allow you to customize what level and kind of error feedback is given, ranging from simple notices to customized functions returned during errors. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="errorfunc.requirements" >需求</A ></H1 ><P >要编译本扩展模块无需外部库文件。</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="errorfunc.installation" >安装</A ></H1 ><P >本扩展模块作为 PHP 内核的一部分,无需安装即可使用。</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="errorfunc.configuration" >运行时配置</A ></H1 ><P >这些函数的行为受 <TT CLASS="filename" >php.ini</TT > 的影响。</P ><P > <DIV CLASS="table" ><A NAME="AEN46911" ></A ><P ><B >表 1. Errors and Logging Configuration Options</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><COL><THEAD ><TR ><TH >Name</TH ><TH >Default</TH ><TH >Changeable</TH ><TH >Changelog</TH ></TR ></THEAD ><TBODY ><TR ><TD >error_reporting</TD ><TD >NULL</TD ><TD >PHP_INI_ALL</TD ><TD > </TD ></TR ><TR ><TD >display_errors</TD ><TD >"1"</TD ><TD >PHP_INI_ALL</TD ><TD > </TD ></TR ><TR ><TD >display_startup_errors</TD ><TD >"0"</TD ><TD >PHP_INI_ALL</TD ><TD >Available since PHP 4.0.3.</TD ></TR ><TR ><TD >log_errors</TD ><TD >"0"</TD ><TD >PHP_INI_ALL</TD ><TD > </TD ></TR ><TR ><TD >log_errors_max_len</TD ><TD >"1024"</TD ><TD >PHP_INI_ALL</TD ><TD >Available since PHP 4.3.0.</TD ></TR ><TR ><TD >ignore_repeated_errors</TD ><TD >"0"</TD ><TD >PHP_INI_ALL</TD ><TD >Available since PHP 4.3.0.</TD ></TR ><TR ><TD >ignore_repeated_source</TD ><TD >"0"</TD ><TD >PHP_INI_ALL</TD ><TD >Available since PHP 4.3.0.</TD ></TR ><TR ><TD >report_memleaks</TD ><TD >"1"</TD ><TD >PHP_INI_ALL</TD ><TD >Available since PHP 4.3.0.</TD ></TR ><TR ><TD >track_errors</TD ><TD >"0"</TD ><TD >PHP_INI_ALL</TD ><TD > </TD ></TR ><TR ><TD >html_errors</TD ><TD >"1"</TD ><TD >PHP_INI_ALL</TD ><TD >PHP_INI_SYSTEM in PHP <= 4.2.3. Available since PHP 4.0.2.</TD ></TR ><TR ><TD >docref_root</TD ><TD >""</TD ><TD >PHP_INI_ALL</TD ><TD >Available since PHP 4.3.0.</TD ></TR ><TR ><TD >docref_ext</TD ><TD >""</TD ><TD >PHP_INI_ALL</TD ><TD >Available since PHP 4.3.2.</TD ></TR ><TR ><TD >error_prepend_string</TD ><TD >NULL</TD ><TD >PHP_INI_ALL</TD ><TD > </TD ></TR ><TR ><TD >error_append_string</TD ><TD >NULL</TD ><TD >PHP_INI_ALL</TD ><TD > </TD ></TR ><TR ><TD >error_log</TD ><TD >NULL</TD ><TD >PHP_INI_ALL</TD ><TD > </TD ></TR ><TR ><TD >warn_plus_overloading</TD ><TD >NULL</TD ><TD >PHP_INI??</TD ><TD > </TD ></TR ></TBODY ></TABLE ></DIV > 有关 PHP_INI_* 常量进一步的细节与定义参见<A HREF="ini.html" >附录 H</A >。 </P ><P >以下是配置选项的简要解释。</P ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><A NAME="ini.error-reporting" ></A ><CODE CLASS="parameter" >error_reporting</CODE > <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A ></DT ><DD ><P > Set the error reporting level. The parameter is either an integer representing a bit field, or named constants. The error_reporting levels and constants are described in <A HREF="ref.errorfunc.html#errorfunc.constants" >Predefined Constants</A >, and in <TT CLASS="filename" >php.ini</TT >. To set at runtime, use the <A HREF="function.error-reporting.html" ><B CLASS="function" >error_reporting()</B ></A > function. See also the <A HREF="ref.errorfunc.html#ini.display-errors" >display_errors</A > directive. </P ><P > In PHP 4 and PHP 5 the default value is E_ALL & ~E_NOTICE. This setting does not show <TT CLASS="constant" ><B >E_NOTICE</B ></TT > level errors. You may want to show them during development. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B >Enabling <TT CLASS="constant" ><B >E_NOTICE</B ></TT > during development has some benefits. For debugging purposes: NOTICE messages will warn you about possible bugs in your code. For example, use of unassigned values is warned. It is extremely useful to find typos and to save time for debugging. NOTICE messages will warn you about bad style. For example, $arr[item] is better to be written as $arr['item'] since PHP tries to treat "item" as constant. If it is not a constant, PHP assumes it is a string index for the array. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > In PHP 5 a new error level <TT CLASS="constant" ><B >E_STRICT</B ></TT > is available. As <TT CLASS="constant" ><B >E_STRICT</B ></TT > is not included within <TT CLASS="constant" ><B >E_ALL</B ></TT > you have to explicitly enable this kind of error level. Enabling <TT CLASS="constant" ><B >E_STRICT</B ></TT > during development has some benefits. STRICT messages will help you to use the latest and greatest suggested method of coding, for example warn you about using deprecated functions. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >PHP Constants outside of PHP: </B > Using PHP Constants outside of PHP, like in <TT CLASS="filename" >httpd.conf</TT >, will have no useful meaning so in such cases the <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A > values are required. And since error levels will be added over time, the maximum value (for <TT CLASS="constant" ><B >E_ALL</B ></TT >) will likely change. So in place of <TT CLASS="constant" ><B >E_ALL</B ></TT > consider using a larger value to cover all bit fields from now and well into the future, a numeric value like <TT CLASS="literal" >2147483647</TT >. </P ></BLOCKQUOTE ></DIV ><P > In PHP 3, the default setting is <TT CLASS="literal" >(E_ERROR | E_WARNING | E_PARSE)</TT >, meaning the same thing. Note, however, that since constants are not supported in PHP 3's <TT CLASS="filename" >php3.ini</TT >, the error_reporting setting there must be numeric; hence, it is <TT CLASS="literal" >7</TT >. </P ></DD ><DT ><A NAME="ini.display-errors" ></A ><CODE CLASS="parameter" >display_errors</CODE > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > This determines whether errors should be printed to the screen as part of the output or if they should be hidden from the user. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > This is a feature to support your development and should never be used on production systems (e.g. systems connected to the internet). </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > Although display_errors may be set at runtime (with <A HREF="function.ini-set.html" ><B CLASS="function" >ini_set()</B ></A >), it won't have any affect if the script has fatal errors. This is because the desired runtime action does not get executed. </P ></BLOCKQUOTE ></DIV ></DD ><DT ><A NAME="ini.display-startup-errors" ></A ><CODE CLASS="parameter" >display_startup_errors</CODE > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > Even when display_errors is on, errors that occur during PHP's startup sequence are not displayed. It's strongly recommended to keep display_startup_errors off, except for debugging. </P ></DD ><DT ><A NAME="ini.log-errors" ></A ><CODE CLASS="parameter" >log_errors</CODE > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > Tells whether script error messages should be logged to the server's error log or <A HREF="ref.errorfunc.html#ini.error-log" >error_log</A >. This option is thus server-specific. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > You're strongly advised to use error logging in place of error displaying on production web sites. </P ></BLOCKQUOTE ></DIV ></DD ><DT ><A NAME="ini.log-errors-max-len" ></A ><CODE CLASS="parameter" >log_errors_max_len</CODE > <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A ></DT ><DD ><P > Set the maximum length of log_errors in bytes. In <A HREF="ref.errorfunc.html#ini.error-log" >error_log</A > information about the source is added. The default is 1024 and 0 allows to not apply any maximum length at all. This length is applied to logged errors, displayed errors and also to <A HREF="reserved.variables.html#reserved.variables.phperrormsg" >$php_errormsg</A >. </P ><P >当使用 <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A > 类型时,其值以字节为度量单位。还可以用简化符号,说明见此 <A HREF="faq.using.html#faq.using.shorthandbytes" >FAQ</A >。</P ></DD ><DT ><A NAME="ini.ignore-repeated-errors" ></A ><CODE CLASS="parameter" >ignore_repeated_errors</CODE > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > Do not log repeated messages. Repeated errors must occur in the same file on the same line until <A HREF="ref.errorfunc.html#ini.ignore-repeated-source" >ignore_repeated_source</A > is set true. </P ></DD ><DT ><A NAME="ini.ignore-repeated-source" ></A ><CODE CLASS="parameter" >ignore_repeated_source</CODE > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > Ignore source of message when ignoring repeated messages. When this setting is On you will not log errors with repeated messages from different files or sourcelines. </P ></DD ><DT ><A NAME="ini.report-memleaks" ></A ><CODE CLASS="parameter" >report_memleaks</CODE > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > If this parameter is set to Off, then memory leaks will not be shown (on stdout or in the log). This has only effect in a debug compile, and if <A HREF="ref.errorfunc.html#ini.error-reporting" >error_reporting</A > includes E_WARNING in the allowed list </P ></DD ><DT ><A NAME="ini.track-errors" ></A ><CODE CLASS="parameter" >track_errors</CODE > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > If enabled, the last error message will always be present in the variable <A HREF="reserved.variables.html#reserved.variables.phperrormsg" >$php_errormsg</A >. </P ></DD ><DT ><A NAME="ini.html-errors" ></A ><CODE CLASS="parameter" >html_errors</CODE > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > Turn off HTML tags in error messages. The new format for HTML errors produces clickable messages that direct the user to a page describing the error or function in causing the error. These references are affected by <A HREF="ref.errorfunc.html#ini.docref-root" >docref_root</A > and <A HREF="ref.errorfunc.html#ini.docref-ext" >docref_ext</A >. </P ></DD ><DT ><A NAME="ini.docref-root" ></A ><CODE CLASS="parameter" >docref_root</CODE > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > The new error format contains a reference to a page describing the error or function causing the error. In case of manual pages you can download the manual in your language and set this ini directive to the URL of your local copy. If your local copy of the manual can be reached by '/manual/' you can simply use <KBD CLASS="userinput" >docref_root=/manual/</KBD >. Additional you have to set docref_ext to match the fileextensions of your copy <KBD CLASS="userinput" >docref_ext=.html</KBD >. It is possible to use external references. For example you can use <KBD CLASS="userinput" >docref_root=http://manual/en/</KBD > or <KBD CLASS="userinput" >docref_root="http://landonize.it/?how=url&theme=classic&filter=Landon &url=http%3A%2F%2Fwww.php.net%2F"</KBD > </P ><P > Most of the time you want the docref_root value to end with a slash '/'. But see the second example above which does not have nor need it. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > This is a feature to support your development since it makes it easy to lookup a function description. However it should never be used on production systems (e.g. systems connected to the internet). </P ></BLOCKQUOTE ></DIV ></DD ><DT ><A NAME="ini.docref-ext" ></A ><CODE CLASS="parameter" >docref_ext</CODE > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > See <A HREF="ref.errorfunc.html#ini.docref-root" >docref_root</A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > The value of docref_ext must begin with a dot '.'. </P ></BLOCKQUOTE ></DIV ></DD ><DT ><A NAME="ini.error-prepend-string" ></A ><CODE CLASS="parameter" >error_prepend_string</CODE > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > String to output before an error message. </P ></DD ><DT ><A NAME="ini.error-append-string" ></A ><CODE CLASS="parameter" >error_append_string</CODE > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > String to output after an error message. </P ></DD ><DT ><A NAME="ini.error-log" ></A ><CODE CLASS="parameter" >error_log</CODE > <A HREF="language.types.string.html" ><B CLASS="type" >string</B ></A ></DT ><DD ><P > Name of the file where script errors should be logged. The file should be writable by the web server's user. If the special value <TT CLASS="literal" >syslog</TT > is used, the errors are sent to the system logger instead. On Unix, this means syslog(3) and on Windows NT it means the event log. The system logger is not supported on Windows 95. See also: <A HREF="function.syslog.html" ><B CLASS="function" >syslog()</B ></A >. If this directive is not set, errors are sent to the SAPI error logger. For example, it is an error log in Apache or <TT CLASS="literal" >stderr</TT > in CLI. </P ></DD ><DT ><A NAME="ini.warn-plus-overloading" ></A ><CODE CLASS="parameter" >warn_plus_overloading</CODE > <A HREF="language.types.boolean.html" ><B CLASS="type" >boolean</B ></A ></DT ><DD ><P > If enabled, this option makes PHP output a warning when the plus (<TT CLASS="literal" >+</TT >) operator is used on strings. This is to make it easier to find scripts that need to be rewritten to using the string concatenator instead (<TT CLASS="literal" >.</TT >). This option doesn't exist as of PHP 4. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="errorfunc.constants" >预定义常量</A ></H1 ><P >以下常量作为 PHP 核心的一部分一直有效。</P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > You may use these constant names in <TT CLASS="filename" >php.ini</TT > but not outside of PHP, like in <TT CLASS="filename" >httpd.conf</TT >, where you'd use the bitmask values instead. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="table" ><A NAME="errorlevels" ></A ><P ><B >表 2. Errors and Logging</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><COL><THEAD ><TR ><TH >Value</TH ><TH >Constant</TH ><TH >Description</TH ><TH >Note</TH ></TR ></THEAD ><TBODY ><TR ><A NAME="e-error" ></A ><TD >1</TD ><TD > <TT CLASS="constant" ><B >E_ERROR</B ></TT > (<A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >) </TD ><TD > Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted. </TD ><TD > </TD ></TR ><TR ><A NAME="e-warning" ></A ><TD >2</TD ><TD > <TT CLASS="constant" ><B >E_WARNING</B ></TT > (<A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >) </TD ><TD > Run-time warnings (non-fatal errors). Execution of the script is not halted. </TD ><TD > </TD ></TR ><TR ><A NAME="e-parse" ></A ><TD >4</TD ><TD > <TT CLASS="constant" ><B >E_PARSE</B ></TT > (<A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >) </TD ><TD > Compile-time parse errors. Parse errors should only be generated by the parser. </TD ><TD > </TD ></TR ><TR ><A NAME="e-notice" ></A ><TD >8</TD ><TD > <TT CLASS="constant" ><B >E_NOTICE</B ></TT > (<A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >) </TD ><TD > Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script. </TD ><TD > </TD ></TR ><TR ><A NAME="e-core-error" ></A ><TD >16</TD ><TD > <TT CLASS="constant" ><B >E_CORE_ERROR</B ></TT > (<A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >) </TD ><TD > Fatal errors that occur during PHP's initial startup. This is like an <TT CLASS="constant" ><B >E_ERROR</B ></TT >, except it is generated by the core of PHP. </TD ><TD >since PHP 4</TD ></TR ><TR ><A NAME="e-core-warning" ></A ><TD >32</TD ><TD > <TT CLASS="constant" ><B >E_CORE_WARNING</B ></TT > (<A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >) </TD ><TD > Warnings (non-fatal errors) that occur during PHP's initial startup. This is like an <TT CLASS="constant" ><B >E_WARNING</B ></TT >, except it is generated by the core of PHP. </TD ><TD >since PHP 4</TD ></TR ><TR ><A NAME="e-compile-error" ></A ><TD >64</TD ><TD > <TT CLASS="constant" ><B >E_COMPILE_ERROR</B ></TT > (<A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >) </TD ><TD > Fatal compile-time errors. This is like an <TT CLASS="constant" ><B >E_ERROR</B ></TT >, except it is generated by the Zend Scripting Engine. </TD ><TD >since PHP 4</TD ></TR ><TR ><A NAME="e-compile-warning" ></A ><TD >128</TD ><TD > <TT CLASS="constant" ><B >E_COMPILE_WARNING</B ></TT > (<A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >) </TD ><TD > Compile-time warnings (non-fatal errors). This is like an <TT CLASS="constant" ><B >E_WARNING</B ></TT >, except it is generated by the Zend Scripting Engine. </TD ><TD >since PHP 4</TD ></TR ><TR ><A NAME="e-user-error" ></A ><TD >256</TD ><TD > <TT CLASS="constant" ><B >E_USER_ERROR</B ></TT > (<A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >) </TD ><TD > User-generated error message. This is like an <TT CLASS="constant" ><B >E_ERROR</B ></TT >, except it is generated in PHP code by using the PHP function <A HREF="function.trigger-error.html" ><B CLASS="function" >trigger_error()</B ></A >. </TD ><TD >since PHP 4</TD ></TR ><TR ><A NAME="e-user-warning" ></A ><TD >512</TD ><TD > <TT CLASS="constant" ><B >E_USER_WARNING</B ></TT > (<A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >) </TD ><TD > User-generated warning message. This is like an <TT CLASS="constant" ><B >E_WARNING</B ></TT >, except it is generated in PHP code by using the PHP function <A HREF="function.trigger-error.html" ><B CLASS="function" >trigger_error()</B ></A >. </TD ><TD >since PHP 4</TD ></TR ><TR ><A NAME="e-user-notice" ></A ><TD >1024</TD ><TD > <TT CLASS="constant" ><B >E_USER_NOTICE</B ></TT > (<A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >) </TD ><TD > User-generated notice message. This is like an <TT CLASS="constant" ><B >E_NOTICE</B ></TT >, except it is generated in PHP code by using the PHP function <A HREF="function.trigger-error.html" ><B CLASS="function" >trigger_error()</B ></A >. </TD ><TD >since PHP 4</TD ></TR ><TR ><A NAME="e-strict" ></A ><TD >2048</TD ><TD > <TT CLASS="constant" ><B >E_STRICT</B ></TT > (<A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >) </TD ><TD > Run-time notices. Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code. </TD ><TD >since PHP 5</TD ></TR ><TR ><A NAME="e-recoverable-error" ></A ><TD >4096</TD ><TD > <TT CLASS="constant" ><B >E_RECOVERABLE_ERROR</B ></TT > (<A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >) </TD ><TD > Catchable fatal error. It indicates that a probably dangerous error occured, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handle (see also <A HREF="function.set-error-handler.html" ><B CLASS="function" >set_error_handler()</B ></A >), the application aborts as it was an <TT CLASS="constant" ><B >E_ERROR</B ></TT >. </TD ><TD >since PHP 5.2.0</TD ></TR ><TR ><A NAME="e-all" ></A ><TD >8191</TD ><TD > <TT CLASS="constant" ><B >E_ALL</B ></TT > (<A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >) </TD ><TD > All errors and warnings, as supported, except of level <TT CLASS="constant" ><B >E_STRICT</B ></TT > in PHP < 6. </TD ><TD >6143 in PHP 5.2.x and 2047 previously</TD ></TR ></TBODY ></TABLE ></DIV ><P > The above values (either numerical or symbolic) are used to build up a bitmask that specifies which errors to report. You can use the <A HREF="language.operators.bitwise.html" >bitwise operators</A > to combine these values or mask out certain types of errors. Note that only '|', '~', '!', '^' and '&' will be understood within <TT CLASS="filename" >php.ini</TT >, however, and that no bitwise operators will be understood within <TT CLASS="filename" >php3.ini</TT >. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="errorfunc.examples" >范例</A ></H1 ><P > Below we can see an example of using the error handling capabilities in PHP. We define an error handling function which logs the information into a file (using an XML format), and e-mails the developer in case a critical error in the logic happens. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN47296" ></A ><P ><B >例 1. Using error handling in a script</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// we will do our own error handling<br /></font><font color="#0000BB">error_reporting</font><font color="#007700">(</font><font color="#0000BB">0</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// user defined error handling function<br /></font><font color="#007700">function </font><font color="#0000BB">userErrorHandler</font><font color="#007700">(</font><font color="#0000BB">$errno</font><font color="#007700">, </font><font color="#0000BB">$errmsg</font><font color="#007700">, </font><font color="#0000BB">$filename</font><font color="#007700">, </font><font color="#0000BB">$linenum</font><font color="#007700">, </font><font color="#0000BB">$vars</font><font color="#007700">) <br />{<br /> </font><font color="#FF8000">// timestamp for the error entry<br /> </font><font color="#0000BB">$dt </font><font color="#007700">= </font><font color="#0000BB">date</font><font color="#007700">(</font><font color="#DD0000">"Y-m-d H:i:s (T)"</font><font color="#007700">);<br /><br /> </font><font color="#FF8000">// define an assoc array of error string<br /> // in reality the only entries we should<br /> // consider are E_WARNING, E_NOTICE, E_USER_ERROR,<br /> // E_USER_WARNING and E_USER_NOTICE<br /> </font><font color="#0000BB">$errortype </font><font color="#007700">= array (<br /> </font><font color="#0000BB">E_ERROR </font><font color="#007700">=> </font><font color="#DD0000">'Error'</font><font color="#007700">,<br /> </font><font color="#0000BB">E_WARNING </font><font color="#007700">=> </font><font color="#DD0000">'Warning'</font><font color="#007700">,<br /> </font><font color="#0000BB">E_PARSE </font><font color="#007700">=> </font><font color="#DD0000">'Parsing Error'</font><font color="#007700">,<br /> </font><font color="#0000BB">E_NOTICE </font><font color="#007700">=> </font><font color="#DD0000">'Notice'</font><font color="#007700">,<br /> </font><font color="#0000BB">E_CORE_ERROR </font><font color="#007700">=> </font><font color="#DD0000">'Core Error'</font><font color="#007700">,<br /> </font><font color="#0000BB">E_CORE_WARNING </font><font color="#007700">=> </font><font color="#DD0000">'Core Warning'</font><font color="#007700">,<br /> </font><font color="#0000BB">E_COMPILE_ERROR </font><font color="#007700">=> </font><font color="#DD0000">'Compile Error'</font><font color="#007700">,<br /> </font><font color="#0000BB">E_COMPILE_WARNING </font><font color="#007700">=> </font><font color="#DD0000">'Compile Warning'</font><font color="#007700">,<br /> </font><font color="#0000BB">E_USER_ERROR </font><font color="#007700">=> </font><font color="#DD0000">'User Error'</font><font color="#007700">,<br /> </font><font color="#0000BB">E_USER_WARNING </font><font color="#007700">=> </font><font color="#DD0000">'User Warning'</font><font color="#007700">,<br /> </font><font color="#0000BB">E_USER_NOTICE </font><font color="#007700">=> </font><font color="#DD0000">'User Notice'</font><font color="#007700">,<br /> </font><font color="#0000BB">E_STRICT </font><font color="#007700">=> </font><font color="#DD0000">'Runtime Notice'</font><font color="#007700">,<br /> </font><font color="#0000BB">E_RECOVERABLE_ERROR </font><font color="#007700">=> </font><font color="#DD0000">'Catchable Fatal Error'<br /> </font><font color="#007700">);<br /> </font><font color="#FF8000">// set of errors for which a var trace will be saved<br /> </font><font color="#0000BB">$user_errors </font><font color="#007700">= array(</font><font color="#0000BB">E_USER_ERROR</font><font color="#007700">, </font><font color="#0000BB">E_USER_WARNING</font><font color="#007700">, </font><font color="#0000BB">E_USER_NOTICE</font><font color="#007700">);<br /> <br /> </font><font color="#0000BB">$err </font><font color="#007700">= </font><font color="#DD0000">"<errorentry>\n"</font><font color="#007700">;<br /> </font><font color="#0000BB">$err </font><font color="#007700">.= </font><font color="#DD0000">"\t<datetime>" </font><font color="#007700">. </font><font color="#0000BB">$dt </font><font color="#007700">. </font><font color="#DD0000">"</datetime>\n"</font><font color="#007700">;<br /> </font><font color="#0000BB">$err </font><font color="#007700">.= </font><font color="#DD0000">"\t<errornum>" </font><font color="#007700">. </font><font color="#0000BB">$errno </font><font color="#007700">. </font><font color="#DD0000">"</errornum>\n"</font><font color="#007700">;<br /> </font><font color="#0000BB">$err </font><font color="#007700">.= </font><font color="#DD0000">"\t<errortype>" </font><font color="#007700">. </font><font color="#0000BB">$errortype</font><font color="#007700">[</font><font color="#0000BB">$errno</font><font color="#007700">] . </font><font color="#DD0000">"</errortype>\n"</font><font color="#007700">;<br /> </font><font color="#0000BB">$err </font><font color="#007700">.= </font><font color="#DD0000">"\t<errormsg>" </font><font color="#007700">. </font><font color="#0000BB">$errmsg </font><font color="#007700">. </font><font color="#DD0000">"</errormsg>\n"</font><font color="#007700">;<br /> </font><font color="#0000BB">$err </font><font color="#007700">.= </font><font color="#DD0000">"\t<scriptname>" </font><font color="#007700">. </font><font color="#0000BB">$filename </font><font color="#007700">. </font><font color="#DD0000">"</scriptname>\n"</font><font color="#007700">;<br /> </font><font color="#0000BB">$err </font><font color="#007700">.= </font><font color="#DD0000">"\t<scriptlinenum>" </font><font color="#007700">. </font><font color="#0000BB">$linenum </font><font color="#007700">. </font><font color="#DD0000">"</scriptlinenum>\n"</font><font color="#007700">;<br /><br /> if (</font><font color="#0000BB">in_array</font><font color="#007700">(</font><font color="#0000BB">$errno</font><font color="#007700">, </font><font color="#0000BB">$user_errors</font><font color="#007700">)) {<br /> </font><font color="#0000BB">$err </font><font color="#007700">.= </font><font color="#DD0000">"\t<vartrace>" </font><font color="#007700">. </font><font color="#0000BB">wddx_serialize_value</font><font color="#007700">(</font><font color="#0000BB">$vars</font><font color="#007700">, </font><font color="#DD0000">"Variables"</font><font color="#007700">) . </font><font color="#DD0000">"</vartrace>\n"</font><font color="#007700">;<br /> }<br /> </font><font color="#0000BB">$err </font><font color="#007700">.= </font><font color="#DD0000">"</errorentry>\n\n"</font><font color="#007700">;<br /> <br /> </font><font color="#FF8000">// for testing<br /> // echo $err;<br /><br /> // save to the error log, and e-mail me if there is a critical user error<br /> </font><font color="#0000BB">error_log</font><font color="#007700">(</font><font color="#0000BB">$err</font><font color="#007700">, </font><font color="#0000BB">3</font><font color="#007700">, </font><font color="#DD0000">"/usr/local/php4/error.log"</font><font color="#007700">);<br /> if (</font><font color="#0000BB">$errno </font><font color="#007700">== </font><font color="#0000BB">E_USER_ERROR</font><font color="#007700">) {<br /> </font><font color="#0000BB">mail</font><font color="#007700">(</font><font color="#DD0000">"phpdev@example.com"</font><font color="#007700">, </font><font color="#DD0000">"Critical User Error"</font><font color="#007700">, </font><font color="#0000BB">$err</font><font color="#007700">);<br /> }<br />}<br /><br /><br />function </font><font color="#0000BB">distance</font><font color="#007700">(</font><font color="#0000BB">$vect1</font><font color="#007700">, </font><font color="#0000BB">$vect2</font><font color="#007700">) <br />{<br /> if (!</font><font color="#0000BB">is_array</font><font color="#007700">(</font><font color="#0000BB">$vect1</font><font color="#007700">) || !</font><font color="#0000BB">is_array</font><font color="#007700">(</font><font color="#0000BB">$vect2</font><font color="#007700">)) {<br /> </font><font color="#0000BB">trigger_error</font><font color="#007700">(</font><font color="#DD0000">"Incorrect parameters, arrays expected"</font><font color="#007700">, </font><font color="#0000BB">E_USER_ERROR</font><font color="#007700">);<br /> return </font><font color="#0000BB">NULL</font><font color="#007700">;<br /> }<br /><br /> if (</font><font color="#0000BB">count</font><font color="#007700">(</font><font color="#0000BB">$vect1</font><font color="#007700">) != </font><font color="#0000BB">count</font><font color="#007700">(</font><font color="#0000BB">$vect2</font><font color="#007700">)) {<br /> </font><font color="#0000BB">trigger_error</font><font color="#007700">(</font><font color="#DD0000">"Vectors need to be of the same size"</font><font color="#007700">, </font><font color="#0000BB">E_USER_ERROR</font><font color="#007700">);<br /> return </font><font color="#0000BB">NULL</font><font color="#007700">;<br /> }<br /><br /> for (</font><font color="#0000BB">$i</font><font color="#007700">=</font><font color="#0000BB">0</font><font color="#007700">; </font><font color="#0000BB">$i</font><font color="#007700"><</font><font color="#0000BB">count</font><font color="#007700">(</font><font color="#0000BB">$vect1</font><font color="#007700">); </font><font color="#0000BB">$i</font><font color="#007700">++) {<br /> </font><font color="#0000BB">$c1 </font><font color="#007700">= </font><font color="#0000BB">$vect1</font><font color="#007700">[</font><font color="#0000BB">$i</font><font color="#007700">]; </font><font color="#0000BB">$c2 </font><font color="#007700">= </font><font color="#0000BB">$vect2</font><font color="#007700">[</font><font color="#0000BB">$i</font><font color="#007700">];<br /> </font><font color="#0000BB">$d </font><font color="#007700">= </font><font color="#0000BB">0.0</font><font color="#007700">;<br /> if (!</font><font color="#0000BB">is_numeric</font><font color="#007700">(</font><font color="#0000BB">$c1</font><font color="#007700">)) {<br /> </font><font color="#0000BB">trigger_error</font><font color="#007700">(</font><font color="#DD0000">"Coordinate $i in vector 1 is not a number, using zero"</font><font color="#007700">, <br /> </font><font color="#0000BB">E_USER_WARNING</font><font color="#007700">);<br /> </font><font color="#0000BB">$c1 </font><font color="#007700">= </font><font color="#0000BB">0.0</font><font color="#007700">;<br /> }<br /> if (!</font><font color="#0000BB">is_numeric</font><font color="#007700">(</font><font color="#0000BB">$c2</font><font color="#007700">)) {<br /> </font><font color="#0000BB">trigger_error</font><font color="#007700">(</font><font color="#DD0000">"Coordinate $i in vector 2 is not a number, using zero"</font><font color="#007700">, <br /> </font><font color="#0000BB">E_USER_WARNING</font><font color="#007700">);<br /> </font><font color="#0000BB">$c2 </font><font color="#007700">= </font><font color="#0000BB">0.0</font><font color="#007700">;<br /> }<br /> </font><font color="#0000BB">$d </font><font color="#007700">+= </font><font color="#0000BB">$c2</font><font color="#007700">*</font><font color="#0000BB">$c2 </font><font color="#007700">- </font><font color="#0000BB">$c1</font><font color="#007700">*</font><font color="#0000BB">$c1</font><font color="#007700">;<br /> }<br /> return </font><font color="#0000BB">sqrt</font><font color="#007700">(</font><font color="#0000BB">$d</font><font color="#007700">);<br />}<br /><br /></font><font color="#0000BB">$old_error_handler </font><font color="#007700">= </font><font color="#0000BB">set_error_handler</font><font color="#007700">(</font><font color="#DD0000">"userErrorHandler"</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// undefined constant, generates a warning<br /></font><font color="#0000BB">$t </font><font color="#007700">= </font><font color="#0000BB">I_AM_NOT_DEFINED</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// define some "vectors"<br /></font><font color="#0000BB">$a </font><font color="#007700">= array(</font><font color="#0000BB">2</font><font color="#007700">, </font><font color="#0000BB">3</font><font color="#007700">, </font><font color="#DD0000">"foo"</font><font color="#007700">);<br /></font><font color="#0000BB">$b </font><font color="#007700">= array(</font><font color="#0000BB">5.5</font><font color="#007700">, </font><font color="#0000BB">4.3</font><font color="#007700">, -</font><font color="#0000BB">1.6</font><font color="#007700">);<br /></font><font color="#0000BB">$c </font><font color="#007700">= array(</font><font color="#0000BB">1</font><font color="#007700">, -</font><font color="#0000BB">3</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// generate a user error<br /></font><font color="#0000BB">$t1 </font><font color="#007700">= </font><font color="#0000BB">distance</font><font color="#007700">(</font><font color="#0000BB">$c</font><font color="#007700">, </font><font color="#0000BB">$b</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// generate another user error<br /></font><font color="#0000BB">$t2 </font><font color="#007700">= </font><font color="#0000BB">distance</font><font color="#007700">(</font><font color="#0000BB">$b</font><font color="#007700">, </font><font color="#DD0000">"i am not an array"</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// generate a warning<br /></font><font color="#0000BB">$t3 </font><font color="#007700">= </font><font color="#0000BB">distance</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">, </font><font color="#0000BB">$b</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="errorfunc.seealso" >参见</A ></H1 ><P > See also <A HREF="function.syslog.html" ><B CLASS="function" >syslog()</B ></A >. </P ></DIV ></DIV ><DIV CLASS="TOC" ><DL ><DT ><B >目录</B ></DT ><DT ><A HREF="function.debug-backtrace.html" >debug_backtrace</A > -- Generates a backtrace</DT ><DT ><A HREF="function.debug-print-backtrace.html" >debug_print_backtrace</A > -- Prints a backtrace </DT ><DT ><A HREF="function.error-get-last.html" >error_get_last</A > -- Get the last occurred error</DT ><DT ><A HREF="function.error-log.html" >error_log</A > -- Send an error message somewhere</DT ><DT ><A HREF="function.error-reporting.html" >error_reporting</A > -- Sets which PHP errors are reported</DT ><DT ><A HREF="function.restore-error-handler.html" >restore_error_handler</A > -- Restores the previous error handler function</DT ><DT ><A HREF="function.restore-exception-handler.html" >restore_exception_handler</A > -- Restores the previously defined exception handler function </DT ><DT ><A HREF="function.set-error-handler.html" >set_error_handler</A > -- Sets a user-defined error handler function</DT ><DT ><A HREF="function.set-exception-handler.html" >set_exception_handler</A > -- Sets a user-defined exception handler function </DT ><DT ><A HREF="function.trigger-error.html" >trigger_error</A > -- Generates a user-level error/warning/notice message</DT ><DT ><A HREF="function.user-error.html" >user_error</A > -- Alias of <A HREF="function.trigger-error.html" ><B CLASS="function" >trigger_error()</B ></A ></DT ></DL ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.enchant-dict-suggest.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.debug-backtrace.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >enchant_dict_suggest</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="funcref.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >debug_backtrace</TD ></TR ></TABLE ></DIV ></BODY ></HTML >