[main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql]

HVCC Home
Blackboard HVCC
Blackboard Manual
Faculty Association

php main
1. what is php
2. http basics
3. php basics
a. embedding php
b. including php
c. php syntax
d. php data types
* e. php variables
4. php expressions
5. php client side
6. php flow control
7. php manual


print version

Note that all external links will open up in a separate window.

This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser.

Directions for surfing with audio.

Variables

These tutorials are about PHP and its use for server-side Web programming.

PHP Variables

variable: a name, or identifier, associated with a value

A variable is just a name assigned to reference a location in memory where some value is stored. It is a shortcut, so that you don't have to refer to memory addresses directly. Since variables are meant to be a people friendly versions of memory locations, we like to pretend that the variables in our programs actually store data. Thinking in computerese is just a little bit easier that way. But you should still know the difference.

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.

The unset() function can also be used to set the value of a variable to null.

Variable Variables and Variable References

PHP allows you to do some neat things with variables. It allows you to create aliases for variables, and it also allows you to have variables whose name is a variable.

A variable reference, or alias, is a variable assigned to refer to the same information as another variable. To assign an alias to a variable, you use the reference operator, which is an equals sign followed by an ampersand ( =& ).

$that = 'abc';   // $that now equals 'abc'
$this =& $that;  // $this now equals 'abc';
$that = 'def';   // both now equal 'def';
$this = 'ghi';   // both now equal 'ghi';

Since they are both now references for the same location in memory, changing the value of one, changes the value of the other. Note however that if you unset() one alias, the other aliases for the same value will not be affected. This is because unset() removes the reference to the memory location for that variable, rather than changing the value stored in that memory location.

You can also use references with functions to return values by reference instead of by value. To do this, you precede the name of the function with an ampersand ( & ) when you define the function, and then use the reference operator to assign its returned value.

Variable variables are variables whose name is a variable. In other words, the name of the variable refers to a memory location that stores the name of the variable that stores the information you want. (Hope you paid attention to pointers in your whatever programming 101 course you learned C or C++ in.) They are represented by adding an additional dollar sign in front of the variable so that the name gets parsed twice.

$foo_bar = "abc";
$tip = "foo_bar";
echo $tip;    // writes out 'foo_bar'
echo $$tip    // writes out 'abc'

Sometimes curly brackets are used to make the code a little clearer. Curly brackets, in PHP are grouping elements. By using them with a variable name, you can make it clear what each dollar sign is acting on in the variable name. This makes the code easier to read.

$$tip    // harder to read
${$tip}  // easier to read

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.

  • local
  • global
  • static
  • parameter

In PHP, all variables are local in scope, even the global ones. Anyone who knows what global and local scopes are can now do a double take. What that means is that all global variables not explicitly passed to a function are not accessible to that function.

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. PHP does not have a block level scope. If you want a block level scope, your best bet is to emulate it with a recursive function.

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.

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.

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.

function myFunc($para1, $para2, [...]) {
  // function code
  }

Parameters are also sometimes called arguments. We will discuss them in more detail when we talk about functions.

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. 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. We will discuss 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.

// 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.

[top]