Skip to content

Latest commit

 

History

History
141 lines (123 loc) · 10.5 KB

06-constants.md

File metadata and controls

141 lines (123 loc) · 10.5 KB

Constants

General

A constant is a named value. Once defined, the value of the constant can not be changed.

A constant can be defined in one of two ways: as a c-constant using a const-declaration, or as a d-constant by calling the library function define. However, the two approaches differ slightly. Specifically:

  • The name of a c-constant must comply with the lexical grammar for a name while that for a d-constant can contain any source character.
  • If define is able to define the given name, it returns TRUE; otherwise, it returns FALSE.

The constants can only hold a value of a scalar type, an array or a resource.

The library function defined reports if a given name (specified as a string) is defined as a constant. The library function constant returns the value of a given constant whose name is specified as a string.

Examples

const MAX_HEIGHT = 10.5;              // define two (case-sensitive) c-constants
const UPPER_LIMIT = MAX_HEIGHT;
define('COEFFICIENT_1', 2.345, TRUE); // define a case-insensitive d-constant (deprecated)
define('FAILURE', FALSE, FALSE);      // define a case-sensitive d-constant

Context-Dependent Constants

The following constants—sometimes referred to as magic constants—are automatically available to all scripts; their values are not fixed and they are case-insensitive:

Constant Name Description
__CLASS__ string; The name of the current class. From within a trait method, the name of the class in which that trait is used. If the current namespace is other than the default, the namespace name and \ are prepended, in that order. If used outside all classes, the value is the empty string.
__COMPILER_HALT_OFFSET__ int; When the __halt_compiler(); construct is used, this constant contains the byte offset in the source file immediately following the __halt_compiler(); token in this file.
__DIR__ string; The directory name of the script. A directory separator is only appended for the root directory.
__FILE__ string; The full name of the script.
__FUNCTION__ string; Inside a function, the name of the current function exactly as it was declared, with the following prepended: If a named namespace exists, that namespace name followed by "". If used outside all functions, the result is the empty string. For a method, no parent-class prefix is present. (See __METHOD__ and anonymous functions).
__LINE__ int; the number of the current source line.
__METHOD__ string; Inside a method, the name of the current method exactly as it was declared, with the following prepended, in order: If a named namespace exists, that namespace name followed by \; the parent class name or trait name followed by ::. If used outside all methods, the result is the same as for __FUNCTION__.
__NAMESPACE__ string; The name of the current namespace exactly as it was declared. For the default namespace, the result is the empty string.
__TRAIT__ string; The name of the current trait. From within a trait method, the name of the current trait. If used outside all traits, the result is the empty string.

Constant names beginning with __ are reserved for future use by the Engine.

Core Predefined Constants

The following constants are automatically available to all scripts; they are case-sensitive with the exception of NULL, TRUE and FALSE:

Constant Name Description
DEFAULT_INCLUDE_PATH string; the fopen library function include path is used if it is not overridden by the php.ini setting include_path.
E_ALL int; All errors and warnings, as supported.
E_COMPILE_ERROR int; Fatal compile-time errors. This is like an E_ERROR, except that E_COMPILE_ERROR is generated by the scripting engine.
E_COMPILE_WARNING int; Compile-time warnings (non-fatal errors). This is like an E_WARNING, except that E_COMPILE_WARNING is generated by the scripting engine.
E_CORE_ERROR int; Fatal errors that occur during PHP's initial start-up. This is like an E_ERROR, except that E_CORE_ERROR is generated by the core of PHP.
E_CORE_WARNING int; Warnings (non-fatal errors) that occur during PHP's initial start-up. This is like an E_WARNING, except that E_CORE_WARNING is generated by the core of PHP.
E_DEPRECATED int; Deprecation notices. Enable this to receive warnings about code that will not work in future versions.
E_ERROR int; Fatal run-time errors. These indicate errors that cannot be recovered from, such as a memory allocation problem. Execution of the script is halted.
E_NOTICE int; Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script.
E_PARSE int; Compile-time parse errors.
E_RECOVERABLE_ERROR int; Catchable fatal error. It indicates that a probably dangerous error occurred, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handler (see the library function set_error_handler), the application aborts as it was an E_ERROR.
E_STRICT int; Have PHP suggest changes to the source code to ensure the best interoperability.
E_USER_DEPRECATED int; User-generated error message. This is like an E_DEPRECATED, except that E_USER_DEPRECATED is generated in PHP code by using the library function trigger_error.
E_USER_ERROR int; User-generated error message. This is like an E_ERROR, except that E_USER_ERROR is generated in PHP code by using the library function trigger_error.
E_USER_NOTICE int; User-generated warning message. This is like an E_NOTICE, except that E_USER_NOTICE is generated in PHP code by using the library function trigger_error.
E_USER_WARNING int; User-generated warning message. This is like an E_WARNING, except that E_USER_WARNING is generated in PHP code by using the library function trigger_error.
E_WARNING int; Run-time warnings (non-fatal errors). Execution of the script is not halted.
FALSE bool; the case-insensitive Boolean value FALSE.
INF float; Infinity
M_1_PI float; 1/pi
M_2_PI float; 2/pi
M_2_SQRTPI float; 2/sqrt(pi)
M_E float; e
M_EULER float; Euler constant
M_LN10 float; log_e 10
M_LN2 float; log_e 2
M_LNPI float; log_e(pi)
M_LOG10E float; log_10 e
M_LOG2E float; log_2 e
M_PI float; Pi
M_PI_2 float; pi/2
M_PI_4 float; pi/4
M_SQRT1_2 float; 1/sqrt(2)
M_SQRT2 float; sqrt(2)
M_SQRT3 float; sqrt(3)
M_SQRTPI float; sqrt(pi)
NAN float; Not-a-Number
NULL null; the case-insensitive value NULL.
PHP_BINARY string; the PHP binary path during script execution.
PHP_BINDIR string; the installation location of the binaries.
PHP_CONFIG_FILE_PATH string; location from which php.ini values were parsed
PHP_CONFIG_FILE_SCAN_DIR string; The directory containing multiple INI files, all of which were parsed on start-up.
PHP_DEBUG int; Indicates whether the engine was built with debugging enabled.
PHP_EOL string; the end-of-line terminator for this platform.
PHP_EXTENSION_DIR string; The directory to be searched by the library function dl when looking for runtime extensions.
PHP_EXTRA_VERSION string; the current PHP extra version.
PHP_INT_MAX int; the maximum representable value for an integer.
PHP_INT_MIN int; the minimum representable value for an integer.
PHP_INT_SIZE int; the number of bytes used to represent an integer.
PHP_FLOAT_DIG int; the number of decimal digits that can be rounded into a float and back without precision loss.
PHP_FLOAT_EPSILON float; the smallest representable positive number x, so that x + 1.0 != 1.0.
PHP_FLOAT_MIN float; the smallest representable normalized floating point number larger than zero.
PHP_FLOAT_MAX float; the largest representable floating point number.
PHP_MAJOR_VERSION int; the current PHP major version
PHP_MANDIR string; the installation location of the manual pages.
PHP_MAXPATHLEN int; the maximum length of a fully qualified filename supported by this build.
PHP_MINOR_VERSION int; the current PHP minor version.
PHP_OS string; the current operating system.
PHP_OS_FAMILY string; the operating system family PHP was built for. Either of 'Windows', 'BSD', 'Darwin', 'Solaris', 'Linux' or 'Unknown'.
PHP_PREFIX string; the value to which "--prefix" was set when configured.
PHP_RELEASE_VERSION int; the current PHP release version.
PHP_ROUND_HALF_DOWN int; Round halves down.
PHP_ROUND_HALF_EVEN int; Round halves to even numbers.
PHP_ROUND_HALF_ODD int; Round halves to odd numbers.
PHP_ROUND_HALF_UP int; Round halves up.
PHP_SAPI string; the Server API for this build.
PHP_SHLIB_SUFFIX string; build-platform's shared library suffix.
PHP_SYSCONFDIR string; the PHP system configuration directory.
PHP_VERSION string; the current PHP version in the form "major.minor.release[extra]".
PHP_VERSION_ID int; the current PHP version.
PHP_ZTS int; Indicates whether the compiler was built with thread safety enabled.
STDIN resource; File resource that maps to standard input (php://stdin).
STDOUT resource; File resource that maps to standard output (php://stdout).
STDERR resource; File resource that maps to standard error (php://stderr).
TRUE bool; the case-insensitive Boolean value TRUE.

The members of the E_* family have values that are powers of 2, so they can be combined meaningfully using bitwise operators.

User-Defined Constants

A constant may be defined inside or outside of functions, inside a class, or inside an interface.