简体   繁体   中英

How to setup environment variables for PHP?

I've just got into PHP world and I have this problem. I want to setup variables for different environment for example, development, CI, staging and production. The example of the variables would be, TWITTER_CALLBACK , TWITTER_KEY , TWITTER_CONSUME , DATABASE_HOST_NAME , DATABASE_PASSWORD , etc.

From ruby or java would I could use yaml file or properties file and restart server based on my environment but for PHP I have no idea.

I'm using PHP 5 and Apache2 on Ubuntu.

You can create a config file like config.php or local_settings .php or any name you like.

Then at the entry point of your project you can include this.

All the constants can be defined in config file and can change for diffrent installs.

Sankalp is right. You can have a config file and it can check the environment by looking at the domain (dev., www.) cf $_SERVER['SERVER_NAME'].

Or let Apache do the work in the virtualhost:

SetEnv VARIABLE value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM