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


基础教程推荐
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01