super-global contains an element for each environment variable present.
are values from the underlying operating system, and those available to PHP are from the environment in which PHP and your web server is running.
environment variable provides your system with a list of locations to search for an executable program. Each location is checked in
until the program is found or there are no more locations left to try, when an error occurs.
Finding the current value of the path is as simple as using the following statement:
On a Unix/Linux system it may look like the following:
On a Windows system, however, it may look like this:
Notice that the format is considerably different for the different operating systems. The Unix/Linux version uses colons to separate the locations and forward
in pathnames, and the Windows version uses semicolons and backslashes. For this reason, PHP provides the host-specific constants
, which enable you to find the appropriate symbols to use for each of these.
In many cases, resetting the
value is specific to the underlying platform; for instance, even if you use the correct
will not exist on a Linux server. However, this allows you to add the current working directory, or one relative to it, to the path
The following example adds the directory
, relative to the current location, to the start of the system path:
$newpath = getcwd() . DIRECTORY_SEPARATOR . "bin" .
PATH_SEPARATOR . $_ENV["PATH"];
function takes a single argument in which an environment variable is assigned its new value. This change is not permanent, and the new value is
only until the script ends.
environment variable contains the server's time zone setting. By overriding this value, you can display the time in another part of the world without needing to know the correct offset or perform any date arithmetic.
Most major cities or
of the world have a value for
that is easy to remember or work out (for instance,
). It can also be a value relative to Greenwich Mean Time or some other common time zone, such as
. On most systems, you can find the available time zones by looking at the items in
The script in Listing 18.1 displays the current time in several locations around the globe.
Listing 18.1. Using the
Environment Variable to Change Time Zone
$now = time();
$original_tz = $_ENV["TZ"];
echo "The time now is " . date("H:i:s", $now) . "<br>";
echo "The time on the US West Coast is " .
date("H:i:s", $now) . "<br>";
echo "The time in France is " . date("H:i:s", $now) . "<br>";
echo "The time in Sydney is " . date("H:i:s", $now) . "<br>";
echo "The time in Tokyo is " . date("H:i:s", $now) . "<br>";
Note that Listing 18.1 begins by storing the current time zone value so that it can be restored after you are done changing the value.
Storing the Time
The timestamp is saved to
at the start of Listing 18.1 so that the same value can be passed to each
function. Although the second argument to
can be omitted, if it is omitted, it is possible that the script execution could take place as a second
over, which would produce confusing output.