Understanding Variables

Variablescontainers in which values can be stored and later retrievedare a fundamental building block of any programming language.

For instance, you could have a variable called number that holds the value 5 or a variable called name that holds the value Chris. The following PHP code declares variables with those names and values:

 $number = 5; $name = "Chris"; 

In PHP, a variable name is always prefixed with a dollar sign. If you remember that, declaring a new variable is very easy: You just use an equals symbol with the variable name on the left and the value you want it to take on the right.

Declaring Variables Unlike in some programming languages, in PHP variables do not need to be declared before they can be used. You can assign a value to a new variable name any time you want to start using it.

Variables can be used in place of fixed values throughout the PHP language. The following example uses echo to display the value stored in a variable in the same way that you would display a piece of fixed text:

 $name = "Chris"; echo "Hello, "; echo $name; 

The output produced is
 Hello, Chris 

Naming Variables

The more descriptive your variable names are, the more easily you will remember what they are used for when you come back to a script several months after you write it.

It is not usually a good idea to call your variables $a, $b, and so on. You probably won't remember what each letter stood for, if anything, for long. Good variable names tell exactly what kind of value you can expect to find stored in them (for example, $price or $name).

Case-Sensitivity Variable names in PHP are case-sensitive. For example, $name is a different variable than $Name, and the two could store different values in the same script.

Variable names can contain only letters, numbers, and the underscore character, and each must begin with a letter or underscore. Table 2.1 shows some examples of valid and invalid variable names.

Table 2.1. Examples of Valid and Invalid Variable Names

Valid Variable Names

Invalid Variable Names







Using Underscores Using the underscore character is a handy way to give a variable a name that is made up of two or more words. For example $first_name and $date_of_birth are more readable for having underscores in place.

Another popular convention for combining words is to capitalize the first letter of each wordfor example, $FirstName and $DateOfBirth. If you prefer this style, feel free to use it in your scripts but remember that the capitalization does matter.


When a variable assignment takes place, the value given does not have to be a fixed value. It could be an expressiontwo or more values combined using an operator to produce a result. It should be fairly obvious how the following example works, but the following text breaks it down into its components:

 $sum = 16 + 30; echo $sum; 

The variable $sum takes the value of the expression to the right of the equals sign. The values 16 and 30 are combined using the addition operatorthe plus symbol (+)and the result of adding the two values together is returned. As expected, this piece of code displays the value 46.

To show that variables can be used in place of fixed values, you can perform the same addition operation on two variables:

 $a = 16; $b = 30; $sum = $a + $b; echo $sum; 

The values of $a and $b are added together, and once again, the output produced is 46.

Variables in Strings

You have already seen that text strings need to be enclosed in quotation marks and learned that there is a difference between single and double quotes.

The difference is that a dollar sign in a double-quoted string indicates that the current value of that variable should become part of the string. In a single-quoted string, on the other hand, the dollar sign is treated as a literal character, and no reference is made to any variables.

The following examples should help explain this. In the following example, the value of variable $name is included in the string:

 $name = "Chris"; echo "Hello, $name"; 

This code displays Hello, Chris.

In the following example, this time the dollar sign is treated as a literal, and no variable substitution takes place:

 $name = 'Chris'; echo 'Hello, $name'; 

This code displays Hello, $name.

Sometimes you need to indicate to PHP exactly where a variable starts and ends. You do this by using curly brackets, or braces ({}). If you wanted to display a weight value with a suffix to indicate pounds or ounces, the statement might look like this:

 echo "The total weight is {$weight}lb"; 

If you did not use the braces around $weight, PHP would try to find the value of $weightlb, which probably does not exist in your script.

You could do the same thing by using the concatenation operator, the period symbol, which can be used to join two or more strings together, as shown in the following example:

 echo 'The total weight is ' . $weight . 'lb'; 

The three valuestwo fixed strings and the variable $weightare simply stuck together in the order in which they appear in the statement. Notice that a space is included at the end of the first string because you want the value of $weight to be joined to the word is.

If $weight has a value of 99, this statement will produce the following output:

 The total weight is 99lb 

    Sams Teach Yourself PHP in 10 Minutes
    Sams Teach Yourself PHP in 10 Minutes
    ISBN: 0672327627
    EAN: 2147483647
    Year: 2005
    Pages: 151
    Authors: Chris Newman

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net