mirror of
https://github.com/php/doc-zh.git
synced 2026-03-24 15:12:20 +01:00
215 lines
5.4 KiB
XML
215 lines
5.4 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: a124543dd3f6b1e5567b07420d23899e582514dc Maintainer: daijie Status: ready -->
|
|
<!-- CREDITS: mowangjuanzi, Luffy -->
|
|
<refentry xml:id="function.define" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>define</refname>
|
|
<refpurpose>定义一个常量</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type>bool</type><methodname>define</methodname>
|
|
<methodparam><type>string</type><parameter>constant_name</parameter></methodparam>
|
|
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
|
|
<methodparam choice="opt"><type>bool</type><parameter>case_insensitive</parameter><initializer>&false;</initializer></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
在运行时定义一个常量。
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>constant_name</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
常量名。
|
|
</para>
|
|
<note>
|
|
<para>
|
|
可以用 <function>define</function> 定义保留关键词甚至无效名称的常量,它的值可以(仅可以)通过
|
|
<function>constant</function> 获取。
|
|
不过,不推荐这么做。
|
|
</para>
|
|
</note>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>value</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
常量的值。
|
|
</para>
|
|
<warning>
|
|
<para>
|
|
常量还可以定义为 <type>resource</type> 类型,但并不推荐这样做,因为可能会有不可预知的行为发生。
|
|
</para>
|
|
</warning>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>case_insensitive</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
如果设置为 &true;,则该常量不区分大小写。默认是区分大小写的。比如,
|
|
<literal>CONSTANT</literal> 和 <literal>Constant</literal> 代表了不同的值。
|
|
</para>
|
|
<warning>
|
|
<simpara>
|
|
PHP 7.3.0 起,废弃定义不区分大小写的常量。自 PHP 8.0.0 开始,
|
|
只接受 &false; 值,传递 &true; 将产生警告。
|
|
</simpara>
|
|
</warning>
|
|
<note>
|
|
<para>
|
|
不区分大小写的常量以小写的形式储存。
|
|
</para>
|
|
</note>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
&return.success;
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="changelog">
|
|
&reftitle.changelog;
|
|
<para>
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>8.1.0</entry>
|
|
<entry>
|
|
<parameter>value</parameter> 现在可以是一个对象。
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>8.0.0</entry>
|
|
<entry>
|
|
现在传递 &true; 到 <parameter>case_insensitive</parameter>
|
|
会发出 <constant>E_WARNING</constant>。仍然允许传递 &false;。
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>7.3.0</entry>
|
|
<entry>
|
|
废弃了 <parameter>case_insensitive</parameter>,并将在 8.0.0 版中移除。
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>定义常量</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
define("CONSTANT", "Hello world.");
|
|
echo CONSTANT; // 输出 "Hello world."
|
|
echo Constant; // 输出 "Constant" 并导致 Notice
|
|
|
|
define("GREETING", "Hello you.", true);
|
|
echo GREETING; // 输出 "Hello you."
|
|
echo Greeting; // 输出 "Hello you."
|
|
|
|
// PHP 7 起就可以运行了
|
|
define('ANIMALS', array(
|
|
'dog',
|
|
'cat',
|
|
'bird'
|
|
));
|
|
echo ANIMALS[1]; // 输出 "cat"
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>以保留名称定义常量</title>
|
|
<para>
|
|
本例子说明了以 <link
|
|
linkend="language.constants.magic">魔术常量</link>相同名称定义常量的<emphasis>能力</emphasis>。由于行为结果过于令人迷惑,所以实践中不推荐。
|
|
</para>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
var_dump(defined('__LINE__'));
|
|
var_dump(define('__LINE__', 'test'));
|
|
var_dump(constant('__LINE__'));
|
|
var_dump(__LINE__);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
bool(false)
|
|
bool(true)
|
|
string(4) "test"
|
|
int(5)
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>defined</function></member>
|
|
<member><function>constant</function></member>
|
|
<member>关于<link linkend="language.constants">常量</link>的章节</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
|
|
-->
|