Page 42 - Open Soource Technologies 304.indd
P. 42
Web Technologies-I
Notes Static Scope
Normally when a function terminates, all of its variables are also cleaned up. Sometimes you want
a local variable to persist between instances of a given function. To do this, you use the static
keyword when you first declare the variable. Then each time you call the function, that variable
will still have the information it contained from the last time the function was called.
static $aValueToRemember;
Even though the value persists, the variable is still local to the function.
Parameters
A parameter is a local variable whose value is passed to the function by the calling code. Unlike
other variables, parameters are declared in a parameter list as part of the function declaration.
Parameters are also sometimes called arguments.
2.3.4 Environment Variable
Beyond the variables you declare in your code, PHP has a collection of environment variables, which
are system defined variables that are accessible from anywhere inside the PHP code, inside of
functions or out. We already encountered one in the $GLOBALS array.
All of these environment variables are stored by PHP as arrays. Some you can address directly
by using the name of the index position as a variable name. Other can only be accessed through
their arrays. Which can be accessed which way is heavily dependent on how strict the security
settings are for PHP on your server. The methods of accessing these variables in more detail when
we talk about processing user data, but using the arrays is a good coding practice.
Some of the environment variables include:
$HTTP_SERVER_VARS[ ]
Contains information about the server and the HTTP connection.
$HTTP_COOKIE_VARS[ ]
Contains any cookie data sent back to the server from the client. Indexed by cookie name.
$HTTP_GET_VARS[ ]
Contains any information sent to the server as a search string as part of the URL.
$HTTP_POST_VARS[ ]
Contains any information sent to the server as a POST style posting from a client form.
$HTTP_POST_FILES[ ]
Contains information about any uploaded files.
$HTTP_ENV_VARS[ ]
Contains information about environmental variables on the server.
We will talk about these more when we talk about processing data from the user. However,
if you are not using Apache, you probably want to read the official PHP documentation and
your server documentation to see if your server creates different environmental variables
than Apache.
If you are using Apache, then there are as many as three ways to access the values of the
environment variables. Say, for instance, that a client sends back a cookie to the server named
myFirstCookie. Although the last example below is guaranteed to work across all platforms,
regardless of security settings, the other two will also work on servers with more relaxed
settings.
36 LOVELY PROFESSIONAL UNIVERSITY