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 中定义可在所有文件中使用的全局常量的最佳方法是什么?


基础教程推荐
- 将变量从树枝传递给 js 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- php中的foreach复选框POST 2021-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- php中的PDF导出 2022-01-01