1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-24 07:02:15 +01:00
Files
archived-doc-zh/reference/errorfunc/functions/trigger-error.xml
2024-12-03 10:00:46 +08:00

173 lines
5.2 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 72b70d7c3c3b2b87423641906da2db407c32c3c3 Maintainer: daijie Status: ready -->
<!-- CREDITS: mowangjuanzi, Luffy -->
<refentry xml:id="function.trigger-error" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>trigger_error</refname>
<refpurpose>产生一个用户级别的 error/warning/notice 信息</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>true</type><methodname>trigger_error</methodname>
<methodparam><type>string</type><parameter>message</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>error_level</parameter><initializer><constant>E_USER_NOTICE</constant></initializer></methodparam>
</methodsynopsis>
<para>
用于触发一个用户级别的错误条件,它能结合内置的错误处理器所关联,或者可以使用用户定义的函数作为新的错误处理程序(<function>set_error_handler</function>)。
</para>
<para>
该函数在你运行出现异常时,需要产生一个特定的响应时非常有用。
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>message</parameter></term>
<listitem>
<para>
该 error 的特定错误信息,长度限制在了 1024 个字节。超过 1024 字节的字符都会被截断。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>error_level</parameter></term>
<listitem>
<para>
该 error 所特定的错误类型。仅 <constant>E_USER_<replaceable>*</replaceable></constant> 系列常量对其有效,默认是 <constant>E_USER_NOTICE</constant>
</para>
<warning>
<simpara>
现已弃用传递 <constant>E_USER_ERROR</constant> 作为 <parameter>error_level</parameter>。抛出
<exceptionname>Exception</exceptionname> 或调用 <function>exit</function>
</simpara>
</warning>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&return.true.always;
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
如果 <parameter>error_level</parameter> 不是 <constant>E_USER_ERROR</constant><constant>E_USER_WARNING</constant><constant>E_USER_NOTICE</constant>
<constant>E_USER_DEPRECATED</constant>,此函数将抛出 <classname>ValueError</classname>
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>8.4.0</entry>
<entry>
现已弃用传递 <constant>E_USER_ERROR</constant> 作为 <parameter>error_level</parameter>。抛出
<exceptionname>Exception</exceptionname> 或调用 <function>exit</function>
</entry>
</row>
<row>
<entry>8.4.0</entry>
<entry>
此函数现在返回类型是 <type>true</type> 而不是 <type>bool</type>
</entry>
</row>
<row>
<entry>8.0.0</entry>
<entry>
如果指定 <parameter>error_level</parameter> 无效,此函数现在将抛出 <classname>ValueError</classname>。之前返回 &false;
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>trigger_error</function> 示例</title>
<para>
<function>set_error_handler</function> 可见到更多详细的例子。
</para>
<programlisting role="php">
<![CDATA[
<?php
$password = $_POST['password'] ?? '';
if ($password === '') {
trigger_error("Using an empty password is unsafe", E_USER_WARNING);
}
$hash = password_hash($password, PASSWORD_DEFAULT);
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<warning>
<para>
<parameter>message</parameter>
里的 HTML 实体,并不会被转义。如果错误消息要显示在浏览器里,需要对错误消息使用
<function>htmlentities</function>
</para>
</warning>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>error_reporting</function></member>
<member><function>set_error_handler</function></member>
<member><function>restore_error_handler</function></member>
<member><link linkend="errorfunc.constants">错误级别常量</link></member>
<member><classname>Deprecated</classname> 注解</member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->