I often use PHP built-in web server for my PHP projects during testing and development phase. The PHP projects in many cases using environment variable to define configuration or controlling app behaviour (e.g. running on prod or test environment).
Usual way
Setting environment variable can be done like this on Linux.
export NAME=VALUE
Start server with setting environment variables
If you want to set environment variable during the start of PHP built-in web server just type like this.
MYENV=myvalue php -S 0.0.0.0:8000
For setting multiple environment variables can be done like below.
MYENV=myvalue MYOTHERENV=myothervalue php -S 0.0.0.0:8000
If above not working, try this one as well.
MYENV=myvalue php -d variables_order=EGPCS -S 0.0.0.0:8000
On PHP you can get the environment variable with this code.
<?php
echo getenv('MYENV');
On Windows you can do that via git-bash shell.
Thank you and I hope you enjoy it.
Top comments (3)
If you are macOS user, ServBay.dev is a good tool, especially for the beginners. It handles all PHP, MariaDB, PostgreSQL versions, plus Redis and Memcached. Run multiple PHP instances simultaneously and switch easily. This tool has made my PHP dev simpler. Worth a shot!
thanks for this nice info, just know now about ServBay
It's a new product, but very useful!