The const keyword will not allow anything that may have to be "processed":
$color = "red";
const RED = "This is the color $color"; //Doesn't work
define(strtoupper($color), "This is the color $color") // Works fine
可以用 define() 函数来定义常量,在 PHP 5.3.0 以后,可以使用 const 关键字在类定义之外定义常量。一个常量一旦被定义,就不能再改变或者取消定义。
常量只能包含标量数据(boolean,integer,float 和 string)。可以定义 resource 常量,但应尽量避免,因为会造成不可预料的结果。
可以简单的通过指定其名字来取得常量的值,与变量不同,不应该在常量前面加上 $ 符号。如果常量名是动态的,也可以用函数 constant() 来获取常量的值。用 get_defined_constants() 可以获得所有已定义的常量列表。
Note: 常量和(全局)变量在不同的名字空间中。这意味着例如
TRUE
和 $TRUE 是不同的。
如果使用了一个未定义的常量,PHP 假定想要的是该常量本身的名字,如同用字符串调用它一样(CONSTANT 对应 "CONSTANT")。此时将发出一个 E_NOTICE 级的错误。参见手册中为什么 $foo[bar] 是错误的(除非事先用 define() 将 bar 定义为一个常量)。如果只想检查是否定义了某常量,用 defined() 函数。
常量和变量有如下不同:
Example #1 定义常量
<?php
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
echo Constant; // 输出 "Constant" 并发出一个提示级别错误信息
?>
Example #2 使用关键字 const 定义常量
<?php
// 以下代码在 PHP 5.3.0 后可以正常工作
const CONSTANT = 'Hello World';
echo CONSTANT;
?>
Note:
和使用 define() 来定义常量相反的是,使用 const 关键字定义常量必须处于最顶端的作用区域,因为用此方法是在编译时定义的。这就意味着不能在函数内,循环内以及 if 语句之内用 const 来定义常量。
参见类常量。
The const keyword will not allow anything that may have to be "processed":
$color = "red";
const RED = "This is the color $color"; //Doesn't work
define(strtoupper($color), "This is the color $color") // Works fine
Of course associative array declarations are available too.
<?php
//PHP 5.6.x <
const DBDATA = array("user"=>"foobar","password"=>"123456","dbname"=>"xyz");
echo "DB-Username is: " . DBDATA['user']; // foobar
echo "DB-UserPwd is: " . DBDATA['user']; // 123456
//PHP 7.x <
define( "DBDATA", array("user"=>"foobar","password"=>"123456","dbname"=>"xyz"));
echo "DB-Username is: " . DBDATA['user']; // foobar
echo "DB-UserPwd is: " . DBDATA['user']; // 123456
?>
While most constants are only defined in one namespace, the case-insensitive true, false, and null constants are defined in ALL namespaces. So, this is not valid:
<?php namespace false;
const ENT_QUOTES = 'My value';
echo ENT_QUOTES;//Outputs as expected: 'My value'
const FALSE = 'Odd, eh?';//FATAL ERROR! ?>
Fatal error: Cannot redeclare constant 'FALSE' in /Volumes/WebServer/0gb.us/test.php on line 5
Actually, there is a way, to (re)declare FALSE (also: False, false, TRUE, NULL, etc.) constant, even if it is already declared:
<?php
var_dump(constant('I_DONT_EXIST')); // warning "Couldn't find constant I_DONT_EXIST"
var_dump(constant('FALSE')); //no warning, output: bool(false)
define('FALSE', TRUE);
var_dump(constant('FALSE')); //output: bool(true)
// but...
var_dump(constant('false')); // output: bool(false)
var_dump(FALSE); // output: bool(false);
?>