An Introduction to PHP Variables

From Techotopia
Revision as of 14:32, 25 May 2007 by Neil (Talk | contribs) (Assigning a Value to a PHP Variable)

Jump to: navigation, search

A large part of writing scripts and programs involve the handling and manipulation of data. Data can take many forms, ranging from single cahracters to whole words and sentences, to numbers and even true and false values. In object oriented environments such as PHP, the data can also be in the form of an object which is a self contained module which contains various pieces of data of different types.

When working with data values in PHP we need some convenient way to store these values so that we can easily access them and make reference to them whenever necessary. This is where PHP variables come in.

It is often useful to think of variables as computer memory locations where data is to be stored. When declaring a variable in PHP it is assigned a name that can used to reference it in other locations in the PHP script. The value of the variable can be accessed, the value can be changed, and the type of variable can be altered all by reference the name assigned at variable creation time.

Naming and Creating a Variable in PHP

Before learning how to declare a variable in PHP it is first important to understand some rules about variable names (also known as variable naming conventions). All PHP variable names mustr begin with a $. It is this prefix that informs the PHP pre-processor that a variable is being declared. The second character of the name must be either a letter or an underscore (_). The remaining characters must comprise only letters, numbers or underscores. All other characters are deemed to be invalid for use in a variable name.

Let's look at some valid and invalid PHP variable names:


$_myName // valid
$myName  // valid
$__myvar // valid
$myVar21 // valid
$_1Big   // invalid - underscore must be followed by a letter
$1Big    // invalid - must begion with a letter or underscore
$_er-t   // invalid contains non alphanumeric character (-)

Variable names in PHP are case-sensitive. This means that PHP considers $_myVariable to be a completely different variable to one that is named ''$_myvariable.

Assigning a Value to a PHP Variable

Values are assigned to variables using the PHp assigmnet operator. The assigment operator is represented by the = sign. To assigna value to a variable therefore, the variable name is place on the left of the expression, followed by the assignment operator. The value to be assigned is then placed to the right of the assigment operator. Finally the line, as with all line sof PHP code, is termininated with a semi-colon (;). Let's assign the word "Circle" to a variable named myShape:

$myShape = "Circle";

We have now declared a variable with the name myShape and assigned a string value to it of "Apple". We can similarly declare a variable to contain an integer value:

$numberOfShapes =  6;

The above assigment creates a variable named numberOfShapes and assigns it a numeric value of 6.


Accessing PHP Variable Values