Reason to set Environment Variable Path for PHP

  • Avinash
  • 0
  • Sep 08, 2011
  • Tutorial

PHP provides variable which gives some information about the environment in which PHP is running.

On windows to get these Environment varibale populated you must set the PHP path in the environment variable.

So if you have not set this path properly then your PHP might not run. In my case all php code was displaying in browser same as we can see the text file in browser.

We can get these variables from $_SERVER and $_ENV arrays. These two arrays are the PHP superglobal array and can be accessed from any where.

How to Set PHP Environment Variable Path

1) Go to control panel >> System >> Advance System Settings

2) You will get one window as per below image. Click on the Environment Variable button.

3) From the System variable section, scroll down for the variable “Path”, select it and edit.

4) Add (at the end) your path to the PHP folder starting with ; , (like ;c:/PHP)

5) Save environment variable box and restart Apache.

After setting up the Environment Variable path, PHP will able to populate the $_SERVER and $_ENV arrays.

How to Use Environment Variable?

Environment varibales can be accessed via $_SERVER and $_ENV supergloabal arrays. Please see these are super globals so you do not need to declare them as a global.

echo $site_root= $_SERVER["DOCUMENT_ROOT"]; // Do not declare them as a global // as both are super global global $_SERVER; // Above sentence is not valid.
echo $site_root= $_SERVER["DOCUMENT_ROOT"];

// Do not declare them as a global
// as both are super global

global $_SERVER;

// Above sentence is not valid.

Set Environment Variable with PHP

This code block will show how to set the custom environment varibale.

$_ENV["MYVAR"] = "My Env variable"; $myvar = $_ENV["MYVAR"];
 $_ENV["MYVAR"] = "My Env variable";
 $myvar = $_ENV["MYVAR"];

Note: Above variable will be available for particular session only.

Set Environment Variable with .htaccess

Below code is used to set the Environment Variable using .htaccess file.

SetEnv HTTP_MY_ENV "my value"
  SetEnv HTTP_MY_ENV "my value"

Note: Using SetEnv, variable name must start with HTTP_ for the securiry reasons.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by: