10 Years
Discussion Span
Last Post by digital-ether

I want to know what are the usages of $_SERVER and $_ENV in php. Or for what they can be used.

Both $_SERVER and $_ENV give you information on the environment your PHP script is running in.

$_SERVER is an array of useful information about the server, and HTTP Request.

An example use is in constructing the full HTTP Request made to the server as this class does:

Another use would be to get the clients, IP address:


$_ENV contains information about the environment in which the PHP parser is running.
It would probably be more appropriate when running PHP as a shell script under the Command Line (CLI).

How they differ is in that $_SERVER presents information assuming PHP was invoked as a webpage, and thus has info on the HTTP request, and the server environment. (which is the most common use of PHP)
$_ENV is a bit more generic, and the information in it would be dependent on the shell that the PHP parser was executing under.

Try doing a var_dump($_SERVER) and a var_dump($_ENV); in your PHP script to see the actual values they contain when your script executes. If you have shell access to the server running your PHP then try executing the php script from the command line to see how it differs.

Votes + Comments
Awesome, really Impressed with her knowledge.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.