mirror of
https://github.com/php/doc-en.git
synced 2026-03-23 23:32:18 +01:00
91 lines
3.1 KiB
XML
91 lines
3.1 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<sect1 xml:id="language.operators.errorcontrol">
|
|
<title>Error Control Operators</title>
|
|
<titleabbrev>Error Control</titleabbrev>
|
|
<simpara>
|
|
PHP supports one error control operator: the at sign (<literal>@</literal>).
|
|
When prepended to an expression in PHP, any diagnostic error that might
|
|
be generated by that expression will be suppressed.
|
|
</simpara>
|
|
<para>
|
|
If a custom error handler function is set with
|
|
<function>set_error_handler</function>, it will still be called even though
|
|
the diagnostic has been suppressed.
|
|
</para>
|
|
|
|
<warning>
|
|
<para>
|
|
Prior to PHP 8.0.0, the <function>error_reporting</function> called inside the custom error handler
|
|
always returned <literal>0</literal> if the error was suppressed by the <literal>@</literal> operator.
|
|
As of PHP 8.0.0, it returns the value of this (bitwise) expression:
|
|
<literal>E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR | E_PARSE</literal>.
|
|
</para>
|
|
</warning>
|
|
|
|
<simpara>
|
|
Any error message generated by the expression is available in the <literal>"message"</literal>
|
|
element of the array returned by <function>error_get_last</function>.
|
|
The result of that function will change on each error, so it needs to be checked early.
|
|
</simpara>
|
|
<para>
|
|
<example>
|
|
<title>Intentional file error</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$my_file = @file ('non_existent_file') or
|
|
die ("Failed opening file: error was '" . error_get_last()['message'] . "'");
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Suppressing Expressions</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
// this works for any expression, not just functions:
|
|
$value = @$cache[$key];
|
|
// will not issue a notice if the index $key doesn't exist.
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<note>
|
|
<simpara>
|
|
The <literal>@</literal>-operator works only on
|
|
<link linkend="language.expressions">expressions</link>.
|
|
A simple rule of thumb is: if one can take the value of something,
|
|
then one can prepend the <literal>@</literal> operator to it.
|
|
For instance, it can be prepended to variables, functions calls,
|
|
certain language construct calls (e.g. <function>include</function>),
|
|
and so forth.
|
|
It cannot be prepended to function or class definitions,
|
|
or conditional structures such as <literal>if</literal> and
|
|
&foreach;, and so forth.
|
|
</simpara>
|
|
</note>
|
|
<warning>
|
|
<para>
|
|
Prior to PHP 8.0.0, it was possible for the <literal>@</literal> operator
|
|
to disable critical errors that will terminate script execution.
|
|
For example, prepending <literal>@</literal> to a call of a function
|
|
which did not exist, by being unavailable or mistyped, would cause
|
|
the script to terminate with no indication as to why.
|
|
</para>
|
|
</warning>
|
|
|
|
<sect2 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>error_reporting</function></member>
|
|
<member><link linkend="ref.errorfunc">Error Handling and Logging functions</link></member>
|
|
</simplelist>
|
|
</para>
|
|
</sect2>
|
|
</sect1>
|