Muhammed Khalander
Muhammed Khalander

Reputation: 295

Difference between $_SERVER and $_ENV

Is my understand regarding superglobal arrays $_ENV and $_SERVER correct ?

$_ENV: Contains information about environment variables
$_SERVER: Contains information about the server

$_ENV is accessible from both web server and on the command line
$_SERVER is accessible through only web server, not on the command line

Upvotes: 15

Views: 8174

Answers (2)

axiac
axiac

Reputation: 72336

Put this code in a file:

<?php
header('Content-Type: text/plain');

echo('$_ENV[] = '); print_r($_ENV);
echo('$_SERVER[] = '); print_r($_SERVER);

Run it using the command line and the web server and see what you get.

To my surprise, on my computer $_ENV[] is empty on both setups and $_SERVER[] contains the environment variables when the code runs from the CLI.

In general, the outcome depends a lot on the operating system and web server you use.

Upvotes: 5

Sean
Sean

Reputation: 1304

You are half right :)

$_ENV contains information about the environment which the PHP interpreter is running in.

Both $_ENV and $_SERVER are accessible from command line

Upvotes: 4

Related Questions