Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between $_SERVER and $_ENV

Tags:

php

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

like image 324
Muhammed Khalander Avatar asked Oct 02 '15 05:10

Muhammed Khalander


People also ask

What does $_ ENV mean?

$_ENV is another superglobal associative array in PHP. It stores environment variables available to current script. $HTTP_ENV_VARS also contains the same information, but is not a superglobal, and now been deprecated. Environment variables are imported into global namespace.

What is $_ server used for?

$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.

What is $_ server [' Php_self ']?

$_SERVER['PHP_SELF'] Contains the file name of the currently running script. $_SERVER['GATEWAY_INTERFACE'] Contains the version of the Common Gateway Interface being used by the server.

What is $_ server [' Script_name ']?

$_SERVER['SCRIPT_NAME'] is server-side. There are no browser compatibility issues as a result, and there shouldn't be security issues as it simply an indication of what the server is serving for the requested URL (i.e. http://example.com/ and http://example.com/index.php would both result in '/index. php' ).


2 Answers

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.

like image 150
axiac Avatar answered Oct 09 '22 22:10

axiac


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

like image 32
Sean Avatar answered Oct 09 '22 22:10

Sean