mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Eleborated on syntax of constants. Moved some information that was scattered over the constant-related functions to the section on constants.
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@49842 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
62dfa44e7d
commit
a085e1c8b3
2 changed files with 127 additions and 66 deletions
|
@ -144,40 +144,18 @@ echo constant("MAXSIZE"); // same thing as the previous line
|
|||
<title>Description</title>
|
||||
<funcsynopsis>
|
||||
<funcprototype>
|
||||
<funcdef>int <function>define</function></funcdef>
|
||||
<funcdef>bool <function>define</function></funcdef>
|
||||
<paramdef>string <parameter>name</parameter></paramdef>
|
||||
<paramdef>mixed <parameter>value</parameter></paramdef>
|
||||
<paramdef>int
|
||||
<paramdef>bool
|
||||
<parameter><optional>case_insensitive</optional></parameter>
|
||||
</paramdef>
|
||||
</funcprototype>
|
||||
</funcsynopsis>
|
||||
<para>
|
||||
Defines a named constant, which is similar to a variable except:
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<simpara>
|
||||
Constants do not have a dollar sign '$' before them;
|
||||
</simpara>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<simpara>
|
||||
Constants may be accessed anywhere without regard to variable
|
||||
scoping rules;
|
||||
</simpara>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<simpara>
|
||||
Constants may not be redefined or undefined once they have
|
||||
been set; and
|
||||
</simpara>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<simpara>
|
||||
Constants may only evaluate to scalar values.
|
||||
</simpara>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
Defines a named constant. See the
|
||||
<link linkend="language.constants">section on constants</link>
|
||||
for more details.
|
||||
</para>
|
||||
<para>
|
||||
The name of the constant is given by <parameter>name</parameter>;
|
||||
|
@ -186,7 +164,7 @@ echo constant("MAXSIZE"); // same thing as the previous line
|
|||
<para>
|
||||
The optional third parameter
|
||||
<parameter>case_insensitive</parameter> is also available. If the
|
||||
value <emphasis>1</emphasis> is given, then the constant will be
|
||||
value <constant>TRUE</constant> is given, then the constant will be
|
||||
defined case-insensitive. The default behaviour is
|
||||
case-sensitive; i.e. CONSTANT and Constant represent different
|
||||
values.
|
||||
|
@ -198,12 +176,19 @@ echo constant("MAXSIZE"); // same thing as the previous line
|
|||
<?php
|
||||
define ("CONSTANT", "Hello world.");
|
||||
echo CONSTANT; // outputs "Hello world."
|
||||
echo Constant; // outputs "Constant" and issues a notice.
|
||||
|
||||
define ("GREETING", "Hello you.",TRUE);
|
||||
echo GREETING; // outputs "Hello you."
|
||||
echo Greeting; // outputs "Hello you."
|
||||
|
||||
?>
|
||||
</programlisting>
|
||||
</example>
|
||||
</para>
|
||||
<para>
|
||||
<function>Define</function> returns TRUE on success and FALSE if
|
||||
<function>Define</function> returns <constant>TRUE</constant>
|
||||
on success and <constant>FALSE</constant> if
|
||||
an error occurs.
|
||||
</para>
|
||||
<para>
|
||||
|
@ -225,13 +210,14 @@ echo CONSTANT; // outputs "Hello world."
|
|||
<title>Description</title>
|
||||
<funcsynopsis>
|
||||
<funcprototype>
|
||||
<funcdef>int <function>defined</function></funcdef>
|
||||
<funcdef>bool <function>defined</function></funcdef>
|
||||
<paramdef>string <parameter>name</parameter></paramdef>
|
||||
</funcprototype>
|
||||
</funcsynopsis>
|
||||
<para>
|
||||
Returns true if the named constant given by
|
||||
<parameter>name</parameter> has been defined, false otherwise.
|
||||
Returns <constant>TRUE</constant> if the named constant given by
|
||||
<parameter>name</parameter> has been defined,
|
||||
<constant>FALSE</constant> otherwise.
|
||||
<example>
|
||||
<title>Checking Constants</title>
|
||||
<programlisting role="php">
|
||||
|
|
|
@ -2,13 +2,97 @@
|
|||
<title>Constants</title>
|
||||
|
||||
<simpara>
|
||||
PHP defines several constants and provides a mechanism for defining
|
||||
more at run-time. Constants are much like variables, save for the
|
||||
two facts that constants must be defined using the
|
||||
<function>define</function> function, and that they cannot later be
|
||||
redefined to another value.
|
||||
A constant is a identifier (name) for a simple value. As the name
|
||||
suggests, that value cannot change during the execution of the script
|
||||
(the magic constants <constant>__FILE__</constant> and
|
||||
<constant>__LINE__</constant> are the only exception). A constant
|
||||
is case-sensitive by default. By convention constants are always
|
||||
uppercase.
|
||||
</simpara>
|
||||
<simpara>
|
||||
The scope of a constant is global.
|
||||
</simpara>
|
||||
<sect1 id="language.constants.syntax">
|
||||
<title>Syntax</title>
|
||||
<simpara>
|
||||
You can define a constant by using the
|
||||
<function>define</function>-function. Once a constant is defined,
|
||||
it can never be changed or undefined.
|
||||
</simpara>
|
||||
<simpara>
|
||||
Only scalar data (<type>boolean</type>, <type>integer</type>,
|
||||
<type>double</type> and <type>string</type>) can be contained
|
||||
in constants.
|
||||
</simpara>
|
||||
<simpara>
|
||||
You can get the value of a constant by simply specifying its name.
|
||||
Unlike with variables, you should <emphasis>not</emphasis> prepend
|
||||
a constant with a <literal>$</literal>.
|
||||
You can also use the <function>constant</function>-function, for
|
||||
example if the name of the constant is variable.
|
||||
Use <function>get_defined_constants</function> to get a list of
|
||||
all defined constants.
|
||||
</simpara>
|
||||
<note>
|
||||
<simpara>
|
||||
Constants and (global) variables are in a different namespace.
|
||||
This implies that for example <constant>TRUE</constant> and
|
||||
<varname>$TRUE</varname> are generally different.
|
||||
</simpara>
|
||||
</note>
|
||||
<simpara>
|
||||
If you use an undefined constant, PHP assumes that you mean
|
||||
the name of the constant itself. A
|
||||
<link linkend="features.error-handling">notice</link> will be issued
|
||||
when this happens. Use the <function>defined</function>-function if
|
||||
you want to know if a constant is set.
|
||||
</simpara>
|
||||
<para>
|
||||
This are the differences with variables:
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<simpara>
|
||||
Constants do not have a dollar sign (<literal>$</literal>) before them;
|
||||
</simpara>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<simpara>
|
||||
Constants may be defined and accessed anywhere without regard to variable
|
||||
scoping rules;
|
||||
</simpara>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<simpara>
|
||||
Constants may not be redefined or undefined once they have
|
||||
been set; and
|
||||
</simpara>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<simpara>
|
||||
Constants may only evaluate to scalar values.
|
||||
</simpara>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
<example>
|
||||
<title>Defining Constants</title>
|
||||
<programlisting>
|
||||
<?php
|
||||
define("CONSTANT", "Hello world.");
|
||||
echo CONSTANT; // outputs "Hello world."
|
||||
echo Constant; // outputs "Constant" and issues a notice.
|
||||
?>
|
||||
</programlisting>
|
||||
</example>
|
||||
|
||||
</para>
|
||||
|
||||
</sect1>
|
||||
|
||||
<sect1 id="language.constants.predefined">
|
||||
<title>Predefined constants</title>
|
||||
<para>
|
||||
The predefined constants (always available) are:
|
||||
|
||||
|
@ -63,7 +147,7 @@
|
|||
<term>TRUE (case-insensitive)</term>
|
||||
<listitem>
|
||||
<simpara>
|
||||
A true value.
|
||||
A true value (see the <type>boolean</type> type).
|
||||
</simpara>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
@ -72,7 +156,16 @@
|
|||
<term>FALSE (case-insensitive)</term>
|
||||
<listitem>
|
||||
<simpara>
|
||||
A false value.
|
||||
A false value (see the <type>boolean</type> type).
|
||||
</simpara>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term>NULL (case-insensitive)</term>
|
||||
<listitem>
|
||||
<simpara>
|
||||
A null value (see the <type>null</type> type).
|
||||
</simpara>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
@ -114,7 +207,7 @@
|
|||
<listitem>
|
||||
<simpara>
|
||||
Something happened which may or may not be an error. Execution
|
||||
continues. Examples include using an unquoted string as a hash
|
||||
continues. Examples include using an unquoted string as a array
|
||||
index, or accessing a variable which has not been set.
|
||||
</simpara>
|
||||
</listitem>
|
||||
|
@ -135,29 +228,10 @@
|
|||
</para>
|
||||
|
||||
<para>
|
||||
The E_* constants are typically used with the
|
||||
<function>error_reporting</function> function for setting the
|
||||
error reporting level. See all these constants at
|
||||
<link linkend="features.error-handling">Error handling</link>.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
You can define additional constants using the
|
||||
<function>define</function> function.</para>
|
||||
|
||||
<para>
|
||||
Note that these are constants, not C-style macros; only valid
|
||||
scalar data may be represented by a constant.
|
||||
|
||||
<example>
|
||||
<title>Defining Constants</title>
|
||||
<programlisting>
|
||||
<?php
|
||||
define("CONSTANT", "Hello world.");
|
||||
echo CONSTANT; // outputs "Hello world."
|
||||
?>
|
||||
</programlisting>
|
||||
</example>
|
||||
The <literal>E_*</literal> constants are typically used with the
|
||||
<function>error_reporting</function> function for setting the
|
||||
error reporting level. See all these constants at
|
||||
<link linkend="features.error-handling">Error handling</link>.
|
||||
|
||||
<example>
|
||||
<title>Using __FILE__ and __LINE__</title>
|
||||
|
@ -170,9 +244,10 @@ function report_error($file, $line, $message) {
|
|||
report_error(__FILE__,__LINE__, "Something went wrong!");
|
||||
?>
|
||||
</programlisting>
|
||||
</example></para>
|
||||
|
||||
</chapter>
|
||||
</example>
|
||||
</para>
|
||||
</sect1>
|
||||
</chapter>
|
||||
|
||||
<!-- Keep this comment at the end of the file
|
||||
Local variables:
|
||||
|
|
Loading…
Reference in a new issue