📄 php_error.asp
字号:
<table class="ex" id="table21" cellspacing="0" width="100%" border="1">
<tr>
<td>
<pre>error_function(error_level,error_message,
error_file,error_line,error_context)</pre>
</td>
</tr>
</table>
<table class="ex" id="table22" cellspacing="0" cellpadding="3" width="100%" border="1">
<tr>
<th valign="top" align="left" width="20%">Parameter</th>
<th valign="top" align="left" width="80%">Description</th>
</tr>
<tr>
<td valign="top">error_level</td>
<td valign="top">Required. Specifies the error report level for the
user-defined error. Must be a value number. See table below for possible
error report levels</td>
</tr>
<tr>
<td valign="top">error_message</td>
<td valign="top">Required. Specifies the error message for the
user-defined error</td>
</tr>
<tr>
<td valign="top">error_file</td>
<td valign="top">Optional. Specifies the filename in which the error
occurred</td>
</tr>
<tr>
<td valign="top">error_line</td>
<td valign="top">Optional. Specifies the line number in which the error
occurred</td>
</tr>
<tr>
<td valign="top">error_context</td>
<td valign="top">Optional. Specifies an array containing every variable,
and their values, in use when the error occurred</td>
</tr>
</table>
<h2>Error Report levels</h2>
<p>These error report levels are the different types of error the user-defined
error handler can be used for:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table23">
<tr>
<th width="5%" align="left">Value</th>
<th width="30%" align="left">Constant</th>
<th width="65%" align="left">Description</th>
</tr>
<tr>
<td valign="top">2</td>
<td valign="top">E_WARNING</td>
<td valign="top">Non-fatal run-time errors. Execution of the script is not
halted</td>
</tr>
<tr>
<td valign="top">8</td>
<td valign="top">E_NOTICE</td>
<td valign="top">Run-time notices. The script found something that might be
an error, but could also happen when running a script normally</td>
</tr>
<tr>
<td valign="top">256</td>
<td valign="top">E_USER_ERROR</td>
<td valign="top">Fatal user-generated error. This is like an E_ERROR set by
the programmer using the PHP function trigger_error()</td>
</tr>
<tr>
<td valign="top">512</td>
<td valign="top">E_USER_WARNING</td>
<td valign="top">Non-fatal user-generated warning. This is like an E_WARNING
set by the programmer using the PHP function trigger_error()</td>
</tr>
<tr>
<td valign="top">1024</td>
<td valign="top">E_USER_NOTICE</td>
<td valign="top">User-generated notice. This is like an E_NOTICE set by the
programmer using the PHP function trigger_error()</td>
</tr>
<tr>
<td valign="top">4096</td>
<td valign="top">E_RECOVERABLE_ERROR</td>
<td valign="top">Catchable fatal error. This is like an E_ERROR but can be
caught by a user defined handle (see also set_error_handler())</td>
</tr>
<tr>
<td valign="top">8191</td>
<td valign="top">E_ALL</td>
<td valign="top">All errors and warnings, except level E_STRICT (E_STRICT
will be part of E_ALL as of PHP 6.0)</td>
</tr>
</table>
<p>Now lets create a function to handle errors:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table24">
<tr>
<td>
<pre>function customError($errno, $errstr)
{
echo "<b>Error:</b> [$errno] $errstr<br />";
echo "Ending Script";
die();
}</pre>
</td>
</tr>
</table>
<p>The code above is a simple error handling function. When it is triggered, it
gets the error level and an error message. It then outputs the error level and
message and terminates the script.</p>
<p>Now that we have created an error handling function we need to decide when it
should be triggered.</p>
<hr />
<h2>Set Error Handler</h2>
<p>The default error handler for PHP is the built in error handler. We are
going to make the function above the default error handler for the duration of
the script.</p>
<p>It is possible to change the error handler to apply for only some errors,
that way the script can handle different errors in different ways. However, in
this example we are going to use our custom error handler for all errors:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table25">
<tr>
<td>
<pre>set_error_handler("customError");</pre>
</td>
</tr>
</table>
<p>Since we want our custom function to handle all errors, the set_error_handler()
only needed one parameter, a second parameter could be added to specify an error
level.</p>
<h2>Example</h2>
<p>Testing the error handler by trying to output variable that does not exist:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table26">
<tr>
<td>
<pre><?php
//error handler function
function customError($errno, $errstr)
{
echo "<b>Error:</b> [$errno] $errstr";
}</pre>
<pre>//set error handler
set_error_handler("customError");</pre>
<pre>//trigger error
echo($test);
?></pre>
</td>
</tr>
</table>
<p>The output of the code above should be something like this:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table27">
<tr>
<td>
<pre><b>Custom error:</b> [8] Undefined variable: test</pre>
</td>
</tr>
</table>
<br />
<hr />
<h2>Trigger an Error</h2>
<p>In a script where users can input data it is useful to trigger errors when an
illegal input occurs. In PHP, this is done by the trigger_error() function.</p>
<h2>Example</h2>
<p>In this example an error occurs if the "test" variable is bigger than "1":</p>
<table class="ex" cellspacing="0" width="100%" border="1" id="table32">
<tr>
<td>
<pre><?php
$test=2;
if ($test>1)
{
trigger_error("Value must be 1 or below");
}
?></pre>
</td>
</tr>
</table>
<p>The output of the code above should be something like this:</p>
<table class="ex" id="table33" cellspacing="0" width="100%" border="1">
<tr>
<td>
<pre><b>Notice</b>: Value must be 1 or below
in <b>C:\webfolder\test.php</b> on line <b>6</b></pre>
</td>
</tr>
</table>
<p>An error can be triggered anywhere you wish in a script, and by adding a
second parameter, you can specify what error level is triggered.</p>
<p>Possible error types:</p>
<ul>
<li>E_USER_ERROR - Fatal user-generated run-time error. Errors that can not
be recovered from. Execution of the script is halted
</li>
<li>E_USER_WARNING - Non-fatal user-generated run-time warning. Execution of
the script is not halted
</li>
<li>E_USER_NOTICE - Default. User-generated run-time notice. The script
found something that might be an error, but could also happen when running a
script normally </li>
</ul>
<h2>Example</h2>
<p>In this example an E_USER_WARNING occurs if the "test" variable is bigger
than "1". If an E_USER_WARNING occurs we will use our custom error handler
and end the script:</p>
<table class="ex" cellspacing="0" width="100%" border="1" id="table36">
<tr>
<td>
<pre><?php
//error handler function
function customError($errno, $errstr)
{
echo "<b>Error:</b> [$errno] $errstr<br />";
echo "Ending Script";
die();
}</pre>
<pre>//set error handler
set_error_handler("customError",E_USER_WARNING);</pre>
<pre>//trigger error
$test=2;
if ($test>1)
{
trigger_error("Value must be 1 or below",E_USER_WARNING);
}
?></pre>
<pre> </pre>
</td>
</tr>
</table>
<p>The output of the code above should be something like this:</p>
<table class="ex" id="table37" cellspacing="0" width="100%" border="1">
<tr>
<td>
<pre><b>Error:</b> [512] Value must be 1 or below
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -