Environment
Variables
The
$_ENV
super-global contains an element for each environment variable present.
Environment variables
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.
The
PATH
environment variable provides your system with a list of locations to search for an executable program. Each location is checked in
turn
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:
echo $_ENV["PATH"];
On a Unix/Linux system it may look like the following:
/bin:/usr/bin:/usr/X11R6/bin:/home/chris/bin
On a Windows system, however, it may look like this:
C:\WINDOWS\system32;C:\WINDOWS
Notice that the format is considerably different for the different operating systems. The Unix/Linux version uses colons to separate the locations and forward
slashes
in pathnames, and the Windows version uses semicolons and backslashes. For this reason, PHP provides the host-specific constants
DIRECTORY_SEPARATOR
and
PATH_SEPARATOR
, which enable you to find the appropriate symbols to use for each of these.
In many cases, resetting the
PATH
value is specific to the underlying platform; for instance, even if you use the correct
PATH_SEPARATOR
constant,
C:/WINDOWS
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
fairly
easily.
The following example adds the directory
bin
, relative to the current location, to the start of the system path:
$newpath = getcwd() . DIRECTORY_SEPARATOR . "bin" .
PATH_SEPARATOR . $_ENV["PATH"];
putenv("PATH=$newpath");
The
putenv
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
remembered
only until the script ends.
Time Zones
The
TZ
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
regions
of the world have a value for
TZ
that is easy to remember or work out (for instance,
Europe/London
,
US/Pacific
). It can also be a value relative to Greenwich Mean Time or some other common time zone, such as
GMT-8
or
EST
. On most systems, you can find the available time zones by looking at the items in
/usr/share/zoneinfo
.
The script in Listing 18.1 displays the current time in several locations around the globe.
Listing 18.1. Using the
TZ
Environment Variable to Change Time Zone
<?php
$now = time();
$original_tz = $_ENV["TZ"];
echo "The time now is " . date("H:i:s", $now) . "<br>";
putenv("TZ=US/Pacific");
echo "The time on the US West Coast is " .
date("H:i:s", $now) . "<br>";
putenv("TZ=Europe/Paris");
echo "The time in France is " . date("H:i:s", $now) . "<br>";
putenv("TZ=Australia/Sydney");
echo "The time in Sydney is " . date("H:i:s", $now) . "<br>";
putenv("TZ=Asia/Tokyo");
echo "The time in Tokyo is " . date("H:i:s", $now) . "<br>";
putenv("TZ=$original_tz");
?>
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
$now
at the start of Listing 18.1 so that the same value can be passed to each
date
function. Although the second argument to
date
can be omitted, if it is omitted, it is possible that the script execution could take place as a second
ticks
over, which would produce confusing output.
|