All variables in PHP are prefixed with a dollar sign. The dollar sign is not technically part of the variable name, but it is required as the first character for the PHP parser to recognize the variable as such.

Declaring Variables

PHP has no command for declaring a variable. A variable is created the moment you first assign a value to it. Setting a variable functions as its declaration. If you want to create a variable without assigning it a value, then you assign it the value of null.

If you try to read the value of a non-existent variable the value also equates to null. You can test whether a variable exists with the isset() function, which returns true if a variable exists and has been assigned a value. You can also its opposite, the empty() function, which returns true if there is no value assigned or the variable does not exist.

<? if (isset(someVariable)){do something;} ?>

Variable Scope

Scope is the region of the program for which the variable is declared. All variables have scope. The scope of a variable is the portion of the script in which the variable can be referenced. PHP has four different variable scopes.

  1. local
  2. global
  3. static
  4. parameter

Local Scope

A local variable is on that is specific to a given instance of a given function. It is created when the function processing begins and is deleted as soon as the function is completed.

Local scope is specific to functions. It's important to keep in mind where and when a variable exists during the execution of your program. Sometime you may run into a problem and realize you're asking a function to act on a variable that no longer exists.

Global Scope

Global scope refers to any variable that is defined outside of any function. They can be accessed from any part of the program that is not inside a function. We saw the same kind of thing in JavaScript.

To access a global variable from within a function, you can call it into the function with the global keyword.

global $varToInclude;

PHP also stores all global variables in an array called $GLOBALS[ ]. Its index is the name of the variable. This array is accessible from within functions and can be used to update global variables directly.

global $somVar;
$someVar = 'abc'

// is the same as

$GLOBALS["someVar"] = 'abc';

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. This variable is not available to all functions, as a global would be.

Environment Variables

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. Others 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. So if you learn to access these variables one way on one server, it might not work the same way on a different server. You need to be aware that there can be differences in PHP's behavior from one server to another.

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.

Again, the variables that are available can be dependent on the server software that is being used.

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.

// major shortcut
$newVar = $myFirstCookie;

// moderate shortcut
$newVar = $_COOKIE["myFirstCookie"];

// full version
$newVar = $HTTP_COOKIE_VARS["myFirstCookie"];

Currently there is a push to make the second version the new standard, since it is shorter and thus makes the code easier to read. Especially when doing a great deal of cookie or form data processing.

(from here)

Includes

Table of Contents