Tutorial

Reason to set Environment Variable Path for PHP

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 [code]$_SERVER[/code] and [code]$_ENV[/code] 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 [code]$_SERVER[/code] and [code]$_ENV[/code] arrays.

How to Use Environment Variable?

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

[cc lang=”php”]

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.
[/cc]

Set Environment Variable with PHP

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

[cc lang=”php”]

$_ENV[“MYVAR”] = “My Env variable”;
$myvar = $_ENV[“MYVAR”];

[/cc]

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.

[cc lang=”apache”]
SetEnv HTTP_MY_ENV “my value”
[/cc]

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

Shares:

Leave a Reply

Your email address will not be published. Required fields are marked *