用php.ini声明全局变量 [英] Declaring global variable with php.ini

查看:355
本文介绍了用php.ini声明全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将变量保留在php.ini文件中.这样,我们对.net中的web.config进行处理.我喜欢在php.ini中保留一个标志类型变量,并将其用于其他项目.

Is it possible to keep variables in php.ini file. Like that we do with the web.config in .net. I like to keep a flag type variable in the php.ini and use it to different projects.

推荐答案

在纯php.ini文件(或.htaccess对等文件)中无法设置用户级变量.有一些PECL模块确实允许这样做,例如 hidef ( http://pecl.php.net/package/hidef )-尽管这些都需要安装在每个您使用的安装.

It's not possible to set user-level variables within a plain php.ini file (or the .htaccess equivilents). There are some PECL modules that do allow that, such as hidef (http://pecl.php.net/package/hidef) - though these would need to be installed on every installation you use.

使用 auto_prepend_file 包含(或包含)文件很有可能-尽管这将在每个PHP请求中进行.

Including (or pre-including) a file with auto_prepend_file is quite possible - though that would be on every PHP request.

通常要做的是将环境变量设置为Web服务器进程的一部分,可以从PHP读取该变量.在Apache中,使用 SetEnv 模块非常简单.

What is frequently done is setting an environment variable as part of the webserver process, which can be read from PHP. In Apache this is quite easy, with the SetEnv module.

SetEnv PRODUCTION_SERVER 1

并在PHP中访问它:

if ($_ENV['PRODUCTION_SERVER']) {...}  // or getenv('PRODUCTION_SERVER')

这篇关于用php.ini声明全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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