PHP 常量定义

常量定义

PHP>=5.3 支持在全局命名空间和类中使用 const 定义常量。

旧式风格:

define("XOOO", "Value");

// 错误用法。等到PHP>7才支持。
define("XO", [1, 2, 3]);
// 解决:define("XO", json_encode([1, 2, 3]));

新式风格:

const XXOO = "Value";
const XXOO = [1, 2, 3];

const 形式仅适用于常量,不适用于运行时才能求值的表达式:

constant 用法

constant()

define("MAXSIZE", 100);
echo MAXSIZE;
echo constant("MAXSIZE"); // same thing as the previous line

const MAXSIZE=100;
echo MAXSIZE;
echo constant("MAXSIZE"); // same thing as the previous line

常量的命名空间

The following code will define two constants in the "test" namespace.

namespace test;
define('testHELLO', 'Hello world!');
define(__NAMESPACE__ . 'GOODBYE', 'Goodbye cruel world!');

print_r(testHELLO);
原文地址:https://www.cnblogs.com/brookin/p/6892140.html