1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
<?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>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
/* Intentional file error */
$my_file = @file ('non_existent_file') or
die ("Failed opening file: error was '" . error_get_last()['message'] . "'");
// this works for any expression, not just functions:
$value = @$cache[$key];
// will not issue a notice if the index $key doesn't exist.
?>
]]>
</programlisting>
</informalexample>
</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>
|