PHP 常量
常量的值是在脚本执行期间不能改变,常量是区分大小写的。
规范中常量标识符总是大写的,常量的作用域是全局的。
合法的常量名以字母或下划线开始,后面跟着任何字母,数字或下划线。
语法
在PHP 5.3.0 之后的版本中,可以用 define() 函数,或 const 两种方法来定义一个常量。
<?php // 合法的常量名 define("FOO", "something"); define("FOO2", "something else"); define("FOO_BAR", "something more"); // 非法的常量名 define("2FOO", "something"); // 下面的定义是合法的,但自定义常量不要以__开头 define("__FOO__", "something"); // 以下代码在 PHP 5.3.0 后可以正常工作 const CONSTANT = 'Hello World';
魔术常量
PHP 提供了大量的预定义常量,有八个魔术常量的值随着它们在代码中的位置改变而改变。
名称 | 说明 |
---|---|
__LINE__ | 文件中的当前行号。 |
__FILE__ | 文件的完整路径和文件名。如果用在被包含文件中,则返回被包含的文件名。 |
__DIR__ | 文件所在的目录。如果用在被包括文件中,则返回被包括的文件所在的目录。它等价于 dirname(__FILE__)。除非是根目录,否则目录中名不包括末尾的斜杠。 |
__FUNCTION__ | 当前函数的名称。匿名函数则为 {closure}。 |
__CLASS__ | 当前类的名称。类名包括其被声明的作用区域(例如 Foo\Bar)。注意自 PHP 5.4 起 __CLASS__ 对 trait 也起作用。当用在 trait 方法中时,__CLASS__ 是调用 trait 方法的类的名字。 |
__TRAIT__ | Trait 的名字。Trait 名包括其被声明的作用区域(例如 Foo\Bar)。 |
__METHOD__ | 类的方法名。 |
__NAMESPACE__ | 当前命名空间的名称。 |
ClassName::class | 完整的类名,参见 ::class。 |
名称 | 说明 |
---|---|
PHP_VERSION (string) | 以 "主版本.次版本.发布版本[额外版本]"的格式表示当前PHP版本,如7.4.16。 |
PHP_MAJOR_VERSION (int) | 当前 PHP 的主版本号(例如,版本号"7.4.16"将返回int(7))。自 PHP 5.2.7起可用。 |
PHP_MINOR_VERSION (int) | 当前 PHP 的次版本号(例如,版本号"7.4.16"将返回int(4))。自 PHP 5.2.7起可用。 |
PHP_RELEASE_VERSION (int) | 当前 PHP 的发布版本号(例如,版本号"7.4.16"将返回int(16))。自 PHP 5.2.7起可用。 |
PHP_EXTRA_VERSION (string) | 当前 PHP 的发布版本号(例如,版本号"7.4.16-extra"将返回'-extra')。自 PHP 5.2.7起可用。 |
PHP_VERSION_ID (int) | 当前 PHP 版本号的整数形式,用于比较版本(例如,版本号"7.4.16"将返回int(70416))。自 PHP 5.2.7起可用。 |
PHP_ZTS (int) | 是否线程安全。自 PHP 5.2.7起可用。 |
PHP_DEBUG (int) | 是否调试。自 PHP 5.2.7起可用。 |
PHP_MAXPATHLEN (int) | 当前 PHP 支持的文件名最大长度(包括路径)。自 PHP 5.3.0起可用。 |
PHP_OS (string) | 当前 PHP 构建时的操作系统。 |
PHP_OS_FAMILY (string) | 当前 PHP 构建时的操作系统族。'Windows','BSD','Darwin','Solaris','Linux' 或者 'Unknown'之一。自 PHP 7.2.0起可用。 |
PHP_SAPI (string) | The Server API for this build of PHP.参见 php_sapi_name()。 |
PHP_EOL (string) | 当前平台中的换行符。自 PHP 5.0.2 起可用。 |
PHP_INT_MAX (int) | 当前 PHP 版本支持的最大整型数字。在 32 位系统中通常为 int(2147483647),64 位系统中为 int(9223372036854775807)。自 PHP 5.0.5 起可用。 |
PHP_INT_MIN (int) | 当前 PHP 版本支持的最大整型数字。在 32 位系统中通常为 int(-2147483648),64 系统中为 int(-9223372036854775808)。自 PHP 7.0.0 起可用。通常情况下 PHP_INT_MIN === ~PHP_INT_MAX。 |
PHP_INT_SIZE (int) | 整型占用的字节数。自 PHP 5.0.5 起可用 |
PHP_FLOAT_DIG (int) | Number of decimal digits that can be rounded into a float and back without precision loss. Available as of PHP 7.2.0. |
PHP_FLOAT_EPSILON (float) | Smallest representable positive number x, so that x + 1.0 != 1.0. Available as of PHP 7.2.0. |
PHP_FLOAT_MIN (float) | Smallest representable positive floating point number. If you need the smallest representable negative floating point number, use - PHP_FLOAT_MAX. Available as of PHP 7.2.0. |
PHP_FLOAT_MAX (float) | Largest representable floating point number. Available as of PHP 7.2.0. |
DEFAULT_INCLUDE_PATH (string) | |
PEAR_INSTALL_DIR (string) | |
PEAR_EXTENSION_DIR (string) | |
PHP_EXTENSION_DIR (string) | |
PHP_PREFIX (string) | The value "--prefix" was set to at configure. |
PHP_BINDIR (string) | Specifies where the binaries were installed into. |
PHP_BINARY (string) | Specifies the PHP binary path during script execution. Available since PHP 5.4. |
PHP_MANDIR (string) | Specifies where the manpages were installed into. Available since PHP 5.3.7. |
PHP_LIBDIR (string) | |
PHP_DATADIR (string) | |
PHP_SYSCONFDIR (string) | |
PHP_LOCALSTATEDIR (string) | |
PHP_CONFIG_FILE_PATH (string) | |
PHP_CONFIG_FILE_SCAN_DIR (string) | |
PHP_SHLIB_SUFFIX (string) | The build-platform's shared library suffix, such as "so" (most Unixes) or "dll" (Windows). |
PHP_FD_SETSIZE (string) | The maximum number of file descriptors for select system calls. Available as of PHP 7.1.0. |