PHP define()函數定義了運行時的常量,
具體語法如下所示:
(PHP 4, PHP 5, PHP 7)
define — Defines a named constant
bool define ( string $name , mixed $value [, bool $case_insensitive = FALSE ] )
Defines a named constant at runtime.
define() 函數的參數說明:
$name 表示常量名稱,
$value 表示對應的常量值,在PHP5版本中,常量值只能是integer, float, string, boolean, or NULL這幾種類型的值,
到了PHP7,常量值可以為數組,
$case_insensitive 代表常量名是否區分大小寫,默認為FALSE時,是不區分大小寫的,設置為TRUE時表示區分大小寫。
define() 的返回值為true時表示常量定義成功,為false時表示定義失敗。
Example:
<?php define("CONSTANT", "Hello world."); echo CONSTANT; // outputs "Hello world." echo Constant; // outputs "Constant" and issues a notice. define("GREETING", "Hello you.", true); echo GREETING; // outputs "Hello you." echo Greeting; // outputs "Hello you." // Works as of PHP 7 define('ANIMALS', array( 'dog', 'cat', 'bird' )); echo ANIMALS[1]; // outputs "cat" ?>
