在 PHP 中定义可在所有文件中使用的全局常量的最佳方法是什么? [英] What is the best way to define a global constant in PHP which is available in all files?

查看:34
本文介绍了在 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 中定义可在所有文件中使用的全局常量的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆