Home » Php » php – Difference between $_SERVER and $_ENV

php – Difference between $_SERVER and $_ENV

Posted by: admin July 12, 2020 Leave a comment


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

$_ENV: Contains information about environment variables
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

How to&Answers:

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


Put this code in a file:

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.