在 PHP 中定义可在所有文件中使用的全局常量的最佳方法是什么?

2022-01-23 00:00:00 variables global-variables constants php

定义全局常量(即在所有 PHP 文件中都可用)的最佳方法是什么?

What is the best way to define a constant that is global, i.e., available in all PHP files?

更新:常量和类常量有什么区别?它们可以互换使用吗?

UPDATE: What are the differences between constants and class constants? Can they be used interchangeably?

推荐答案

在 top .php 文件中定义常量,该文件将包含在所有其他脚本中.它可能是您的前端控制器、您的配置文件或为此单一目的而创建的文件.

Define your constant in your top .php file, that will be included in all the other scripts. It may be your front controller, your config file, or a file created for this single purpose.

!defined('MY_CONST') && define('MY_CONST', 'hello');

相关文章