constant


常量一旦被定义,就不能再改变或取消定义,常量的作用域是全局,可以在任何位置访问

  • define 是函数
  • const 是关键字

define


举例说明

define('CONSTANT', 'this is test');//定义一个常量
echo CONSTANT;
----------------------------------------------------
$constantVariable= 'TEST';//通过变量定义常量名
define($constantVariable, 'this is test');
echo TEST;//不可行,报错
echo constant($constantVariable);//正确获取方式
this is test
----------------------------------------------------
class test {
  public $constant = define();//不可行
  define();//不可行
  function test() {
    define('CONSTANT', 'this is test');//定义一个常量
  }
}

const


举例说明

$variable = 200;
const ERROR_CODE = $varable;//不可以通过变量赋值
----------------------------------------------------------
const ERROR_CODE = 200;//不建议这样定义常量,使用define更常见
echo ERROR_CODE;
200;
-----------------------------------------------------------
class name {
    const ERROR_CODE = 200;
    public function getConstant() {
        self::ERROR_CODE;//在本类中获取定义的常量
    }
}
echo name::ERROR_CODE;//直接获取类中定义的常量
-----------------------------------------------------------
use name;
name::ERROR_CODE;//直接获取类中定义的常量

系统预定义常量


系统预定义常量 常量值

results matching ""

    No results matching ""