PHP define() 定义常量

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版本中,常量值只能是integerfloatstringboolean, 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"

?>

 

posted @ 2018-05-16 04:09  Ryan_zheng  阅读(3309)  评论(0编辑  收藏  举报