DEV Community

Sony AK
Sony AK

Posted on

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

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.

Top comments (3)

Collapse
 
leslieeeee profile image
Leslie

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!

Collapse
 
sonyarianto profile image
Sony AK

thanks for this nice info, just know now about ServBay

Collapse
 
leslieeeee profile image
Leslie

It's a new product, but very useful!