DEV Community

loading...

How to start PHP built-in web server along with setting environment variable

sonyarianto profile image Sony AK ・1 min read

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

For setting multiple environment variables can be done like below.

MYENV=myvalue MYOTHERENV=myothervalue php -S 0.0.0.0:8000
Enter fullscreen mode Exit fullscreen mode

If above not working, try this one as well.

MYENV=myvalue php -d variables_order=EGPCS -S 0.0.0.0:8000
Enter fullscreen mode Exit fullscreen mode

On PHP you can get the environment variable with this code.

<?php
  echo getenv('MYENV');
Enter fullscreen mode Exit fullscreen mode

On Windows you can do that via git-bash shell.

Thank you and I hope you enjoy it.

Discussion

pic
Editor guide