略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: 语法

2024-03-28

语法

可以使用 const 关键字或 define() 函数两种方法来定义一个常量。函数 define() 允许将常量定义为一个表达式,而 const 关键字有一些限制,具体可参见下述章节。一个常量一旦被定义,就不能再改变或者取消定义。

使用 const 关键字定义常量时,只能包含标量数据(boolintfloatstring)。可以将常量定义为一个表达式,也可以定义为一个 array。还可以定义 resource 为常量,但应尽量避免,因为可能会造成不可预料的结果。

可以简单的通过指定其名字来取得常量的值,与变量不同,不应该在常量前面加上 $ 符号。如果常量名是动态的,也可以用函数 constant() 来获取常量的值。用 get_defined_constants() 可以获得所有已定义的常量列表。

注意: 常量和(全局)变量在不同的名字空间中。这意味着例如 true$TRUE 是不同的。

如果使用了一个未定义的常量,则会抛出 Error。 在 PHP 8.0.0 之前,调用未定义的常量会被解释为一个该常量的 字符串,即(CONSTANT 对应 "CONSTANT" )。 此方法已在 PHP 7.2.0 中被废弃,会抛出一个 E_WARNING 级错误。(PHP 7.2.0 之前会发出一个 E_NOTICE 级的错误。)参见手册中为什么 $foo[bar] 是错误的(除非 bar 是一个常量)。这不适用于 (完全)限定的常量,如果未定义,将始终引发 Error

注意: 如果要检查是否定义了某常量,请使用 defined() 函数。

常量和变量有如下不同:

  • 常量前面没有美元符号($);
  • 常量可以不用理会变量的作用域而在任何地方定义和访问;
  • 常量一旦定义就不能被重新定义或者取消定义;
  • 常量只能计算标量值或数组。

示例 #1 定义常量

<?php
define
("CONSTANT""Hello world.");
echo 
CONSTANT// 输出 "Hello world."
echo Constant// 抛出错误:未定义的常量 "Constant"
               // 在 PHP 8.0.0 之前,输出 "Constant" 并发出一个提示级别错误信息
?>

示例 #2 使用关键字 const 定义常量

<?php
// 简单的标量值
const CONSTANT 'Hello World';

echo 
CONSTANT;

// 标量表达式
const ANOTHER_CONST CONSTANT.'; Goodbye World';
echo 
ANOTHER_CONST;

const 
ANIMALS = array('dog''cat''bird');
echo 
ANIMALS[1]; // 将输出 "cat"

// 常量数组
define('ANIMALS', array(
    
'dog',
    
'cat',
    
'bird'
));
echo 
ANIMALS[1]; // 将输出 "cat"
?>

?>

注意:

和使用 define() 来定义常量相反的是,使用 const 关键字定义常量必须处于最顶端的作用域,因为用此方法是在编译时定义的。这就意味着不能在函数内,循环内以及 iftry/catch 语句之内用 const 来定义常量。

参见

add a noteadd a note

User Contributed Notes 6 notes

up
5
souzanicolas87 at gmail dot com
5 months ago
the documentation doesn't go too far in explaining the crucial difference between the two ways of declaring constants in PHP.

Const is handled at compile time, define() at run time. For this reason, a constant cannot be conditionally defined using Const, for example.

Another difference we can notice occurs in the constant declarations in classes. Const infiltrates the class scope, while define() leaks into the global scope.

<?php

Class Myclass (){
    const
NAME = "Nicolas";
}

?>

The NAME constant is within the scope of the MyClass class.
up
8
login at (two)view dot de
4 years ago
Just a quick note:
From PHP7 on you can even define a multidimensional Array as Constant:

define('QUARTLIST',array('1. Quarter'=>array('jan','feb','mar'),'2.Quarter'=>array('may','jun','jul'));

does work as expected.
up
-10
mh at hanft dot de
2 years ago
With PHP 5.6, multi-dimensional arrays are also possible if you use "const" instead of "define". So,

define('QUARTLIST',array('1. Quarter'=>array('jan','feb','mar'),'2.Quarter'=>array('may','jun','jul')));

won't work with PHP 5.6, but

const QUARTLIST=array('1. Quarter'=>array('jan','feb','mar'),'2.Quarter'=>array('may','jun','jul'));

will.
up
-6
LilyWhite
8 months ago
> Constants and (global) variables are in a different namespace. This implies that for example true and $TRUE are generally different.

This means that you may assign a value to a variable with the same name of a constant, which might lead to unwanted results.

<?php
const FOO = "hello";
$FOO = "goodbye";
echo
$FOO;
?>

will output ``goodbye''.
up
-10
privat at ulrich-kapp dot de
2 years ago
const ArrayFromTextfile = file("mytextfile.txt", FILE_IGNORE_NEW_LINES);

does not work, it throws an error:
Fatal error: Constant expression contains invalid operations in php shell code on line …

Instead use:

define ("ArrayFromTextfile", file("mytextfile.txt", FILE_IGNORE_NEW_LINES));

print_r(ArrayFromTextfile);

Result:
Array
(
    [0] ⇨ Line 1
    [1] ⇨ Line 2
    [2] ⇨ Line 3
    [3] => …
)
up
-21
halfhearted13 at gmail dot com
1 year ago
Constants of constant :

<?php

const foo = hello;
const
bar = foo;
const
bd = bar;

echo
"bd = " .bd;   // Prints out: Hello

?>

官方地址:https://www.php.net/manual/en/language.constants.syntax.php

冷却塔厂家 广告
-- 广告
北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3