当前位置: 首页 > 文档资料 > PHP 语言规范 >

06 常量

优质
小牛编辑
124浏览
2023-12-01

概述

常量是一个简单值的标识符(名字)。定义之后,脚本之行期间常量的值不能改变。

常量有俩种定义方法:使用const关键字在类之外定义常量,或者使用define函数。 这俩种方法略有不同,如下:

  • 使用const关键字定义的名称必须遵循语法分析器对一个命名的规则。函数 define 定义的常量可以是任何源字符。
  • const关键字定义的常量是大小写敏感的。函数define的第三个参数可以设置定义的常量是否大小写敏感。
  • 用函数define定义常量成功时反悔TRUE,否则返回FALSE

常量只能包含标量类型、数组或者资源类型

函数defined判断一个名称(字符串)是否已经被定义成一个常量。 函数constant能够返回一个常量的值。

实例

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
define('FAILURE', FALSE, FALSE);      // define a case-sensitive d-constant

上下文相关的常量

以下的常量-被称为魔法常量-在PHP脚本的全局都有效,他们的值不是确定的,而且大小写敏感:

常量名称描述
__CLASS__string; 当前类的名称。在 trait 方法内部, 这个值是 trait 被使用的类的名称.如果当前的命名空间就是默认值,这个值就是命名空间前加一个反斜线\。如果不在任何一个类的内部,这个值就是一个空字符串。
__COMPILER_HALT_OFFSET__int; 当 __halt_compiler(); 使用时, this constant contains the byte offset in the source file immediately following the __halt_compiler(); token in this file.
__DIR__string; 文件所在目录. 根目录的话,该值仅有一个文件分隔符。
__FILE__string; 文件完整的路径和文件名。
__FUNCTION__string; 在函数内部,该值就是声明该函数的定义名称。或许还有前缀: 如果存在命名空间,就在命名空间后加上\。如果不在任何函数内部,这个值就是空字符串。对于一个方法,没有父类前缀就是当前方法。(参考__METHOD__匿名函数))。
__LINE__int; 文件当前行号。
__METHOD__string; 在一个类的方法内部,返回当前的方法名称。而且会带着以下的前缀表示:如果存在命名空间,前缀就是命名空间后面带一个\;父类或者trait名带一个::。如果不在任何方法内部,返回结果同__FUNCTION__
__NAMESPACE__string; 当前命名空间的名称。默认命名空间下,是一个空字符串。
__TRAIT__string; 当前 Trait 的名称。在trait的方法里,返回的夜市当前的trait名称。如果不在任何trait 里,返回空字符串。

以 __ 为前缀的常量都是预留给将来引擎的。

核心预定义常量

以下所有常量在所有文件中都已经被自动引用,均可直接使用。除了NULLTRUEFALSE,都是大小写敏感的。

常量名称描述
DEFAULT_INCLUDE_PATHstring; the fopen library function include path is used if it is not overridden by the php.ini setting include_path.
E_ALLint; All errors and warnings, as supported.
E_COMPILE_ERRORint; Fatal compile-time errors. This is like an E_ERROR, except that E_COMPILE_ERROR is generated by the scripting engine.
E_COMPILE_WARNINGint; 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_ERRORint; 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_WARNINGint; 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_DEPRECATEDint; Deprecation notices. Enable this to receive warnings about code that will not work in future versions.
E_ERRORint; 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_NOTICEint; 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_PARSEint; Compile-time parse errors.
E_RECOVERABLE_ERRORint; 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_STRICTint; Have PHP suggest changes to the source code to ensure the best interoperability.
E_USER_DEPRECATEDint; 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_ERRORint; 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_NOTICEint; 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_WARNINGint; 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_WARNINGint; Run-time warnings (non-fatal errors). Execution of the script is not halted.
E_USER_DEPRECATEDint; User-generated warning message. This is like an E_DEPRECATED, except that E_USER_DEPRECATED is generated in PHP code by using the library function trigger_error.
FALSEbool; the case-insensitive Boolean value FALSE.
INFfloat; Infinity
M_1_PIfloat; 1/pi
M_2_PIfloat; 2/pi
M_2_SQRTPIfloat; 2/sqrt(pi)
M_Efloat; e
M_EULERfloat; Euler constant
M_LN10float; log_e 10
M_LN2float; log_e 2
M_LNPIfloat; log_e(pi)
M_LOG10Efloat; log_10 e
M_LOG2Efloat; log_2 e
M_PIfloat; Pi
M_PI_2float; pi/2
M_PI_4float; pi/4
M_SQRT1_2float; 1/sqrt(2)
M_SQRT2float; sqrt(2)
M_SQRT3float; sqrt(3)
M_SQRTPIfloat; sqrt(pi)
NANfloat; Not-a-Number
NULLnull; the case-insensitive value NULL.
PHP_BINARYstring; the PHP binary path during script execution.
PHP_BINDIRstring; the installation location of the binaries.
PHP_CONFIG_FILE_PATHstring; location from which php.ini values were parsed
PHP_CONFIG_FILE_SCAN_DIRstring; The directory containing multiple INI files, all of which were parsed on start-up.
PHP_DEBUGint; Indicates whether the engine was built with debugging enabled.
PHP_EOLstring; the end-of-line terminator for this platform.
PHP_EXTENSION_DIRstring; The directory to be searched by the library function dl when looking for runtime extensions.
PHP_EXTRA_VERSIONstring; the current PHP extra version.
PHP_INT_MAXint; the maximum representable value for an integer.
PHP_INT_MINint; the minimum representable value for an integer.
PHP_INT_SIZEint; the number of bytes used to represent an integer.
PHP_MAJOR_VERSIONint; the current PHP major version
PHP_MANDIRstring; the installation location of the manual pages.
PHP_MAXPATHLENint; the maximum length of a fully qualified filename supported by this build.
PHP_MINOR_VERSIONint; the current PHP minor version.
PHP_OSstring; the current operating system.
PHP_PREFIXstring; the value to which "--prefix" was set when configured.
PHP_RELEASE_VERSIONint; the current PHP release version.
PHP_ROUND_HALF_DOWNint; Round halves down.
PHP_ROUND_HALF_EVENint; Round halves to even numbers.
PHP_ROUND_HALF_ODDint; Round halves to odd numbers.
PHP_ROUND_HALF_UPint; Round halves up.
PHP_SAPIstring; the Server API for this build.
PHP_SHLIB_SUFFIXstring; build-platform's shared library suffix.
PHP_SYSCONFDIRstring; the PHP system configuration directory.
PHP_VERSIONstring; the current PHP version in the form "major.minor.release[extra]".
PHP_VERSION_IDint; the current PHP version.
PHP_ZTSint; Indicates whether the compiler was built with thread safety enabled.
STDINresource; File resource that maps to standard input (php://stdin).
STDOUTresource; File resource that maps to standard output (php://stdout).
STDERRresource; File resource that maps to standard error (php://stderr).
TRUEbool; 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.

用户定义的常量

在函数内或函数外定义的常量,在内部定义,或者 在接口内部定义。