What is the best way to define a global constant in PHP which is available in all files?(在 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');
这篇关于在 PHP 中定义可在所有文件中使用的全局常量的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHP 中定义可在所有文件中使用的全局常量的最佳方法是什么?


- SoapClient 设置自定义 HTTP Header 2021-01-01
- Laravel 仓库 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01