How to Set the Env Variable for PHP

How to set the env variable for PHP?

You need to put the directory that has php.exe in you WAMP installation into your PATH. It is usually something like C:\wamp\xampp\php

How to set up an env variable in PHP?

The best way to do it is to set your environnements variables inside your... environnement.

If you use Apache, you can add this inside your virtual host configuration (or .htaccess):

SetEnv MYAPP_ENV value

Then, inside your PHP:


There is SetEnv equivalent for all the web servers you can find.

How can I setup an environment variable to hold a DSN for PHP?

Your webserver does not run your shell so it doesn't the .bash_profile. There are various tricks to get environment variables into Apache but they all rely on having administration access to your webserver, and you don't get that on low-end godaddy hosting.

If you were to explain why you are trying to get environment variables into Apache, what type of hosting you have and what access you have to the server we might be able to better advise. There are modules which expose access to manipulate environment variables for Apache (mod_env for example) but these may not be available/appropriate. It's also possible to get data (and code) into PHP from a .htaccess file, but again its impossible to say whether this meets your requirements.

Environment Variable .env Is Not Accessible On Other Pages in PHP via AJAX

You may use session variables to store data in variables which can be accessed in different pages of the system

  1. Say if the variable name is var1, then simply use $_SESSION['var1']
  2. Make sure session_start(); is placed at the top of all the PHP scripts using the session variables


$_SESSION['var1'] = $var1;

// other statements


How to assign environment variables in .env file to $_ENV variable php

You can use this lib for that:

Related Topics

Leave a reply