Using Variables in Your Scripts

Using Variables in Your Scripts

The most important new idea in this chapter is the notion of a variable. A variable is a container for holding information in the computer's memory. To make things easier for the programmer, every variable has a name. You can store information into a variable and get information out of a variable.

Introducing the "Hi Jacob" program

The program featured in Figure 2.3 uses a variable, although you might not be able to tell simply by looking at the output.

Click To expand
Figure 2.3: The word "Jacob" is stored in a variable in this page. You can't really see anything special about this program from the Web page itself (even if you look at the HTML source). To see what's new, look at the source code of hiJacob.php.
<title>Hi Jacob</title>
<h1>Hi Jacob</h1>

<h3>Demonstrates using a variable</h3>


$userName = "Jacob";

print "Hi, $userName";



In regular HTML and JavaScript programming, you can use the "view source" command of the Web browser to examine the code for your programs. For server-side languages, this is not sufficient. There will be no PHP at all in the view source document. Remember that the actual program code never gets to your Web browser. Instead, the program is executed on the server, and the results of the program are sent to the browser as ordinary HTML. Be sure to be looking at the actual PHP source code on the server when you are examining these programs. On a related note, you cannot simply use the File menu of your browser to load a PHP page. Instead, you'll need to run it through a server.

The hiJacob page is mainly HTML with a small patch of PHP code in it. That code does a lot of very important work.

Creating a String Variable

The line $userName = "Jacob"; does a number of things. First, it creates a variable named $userName. In PHP, all variables begin with a dollar sign to distinguish them from other program elements. The variable's name is significant.

Naming Your Variables

As a programmer, you will frequently get to name things. Experienced programmers have learned some tricks about naming variables and other elements.

  • Make the name descriptive. It's much easier to figure out what $userName means than something like $myVariable or $r. When possible, make sure your variable names describe the kind of information they contain.

  • Use an appropriate length. Your variable name should be long enough to be descriptive, but not so long that it becomes tedious to type.

  • Don't use spaces. Most languages (including PHP) don't allow spaces in variable names.

  • Don't use punctuation. Most of the special characters such as #, *, and / already have meaning in programming languages, so they can't be used in variable names. Of course, every variable in PHP begins with the $ character, but otherwise you should avoid using punctuation. One exception to this rule is the underscore (_) character, which is allowed in most languages, including PHP.

  • Be careful about case. PHP is a case-sensitive language, which means that it considers $userName, $USERNAME, and $UserName to be three different variables. The convention in PHP is to use all lowercase except when separating words (note the uppercase "N" in $userName.) This is a good convention to follow, and it's the one I use throughout this book.

  • Watch your spelling! Every time you refer to a variable, PHP checks to see if that variable already exists somewhere in your program. If so, it uses that variable. If not, it quietly makes a new variable for you. If you misspell a variable name, PHP will not catch it. Instead, it will make a whole new variable, and your program probably won't work correctly.

It isn't necessary to explicitly create a variable. When you refer to a variable, it is automatically created by PHP.

Assigning a Value to a Variable

The equals sign (=) is special in PHP. It does not mean "equals" (at least in the present context.) The equals sign is used for assignment. If you read the equals sign as the word "gets," you'll be closer to the meaning PHP uses for this symbol. For example, the line

$userName = "Jacob"

should be read

"The variable $userName gets the value "Jacob."

Usually when you create a variable in PHP, you'll also be assigning some value to it. Assignment flows from right to left.

The $userName variable has been assigned the value "Jacob." Computers are picky about what type of information goes into a variable, but PHP automates this process for you. Still, it's important to recognize that "Jacob" is a text value, because text is stored and processed a little bit differently in computer memory than numeric data.


Computer programmers almost never refer to text as text. Instead, they prefer the more esoteric term string. The word string actually has a somewhat poetic origin, because the underlying mechanism for storing text in a computer's memory reminded early programmers of making a chain of beads on a string.

Printing the Value of a Variable

The next line of code prints a message to the screen. You can print any text to the screen you wish. Text (also called string data) is usually encased in quotes. If you wish to print the value of a variable, simply place the variable name in the text you want printed. The line

print "Hi, $userName";

actually produces the output

Hi, Jacob

because when the server encounters the variable $userName, it replaces it with the value of that variable, which is "Jacob." The output of the PHP program will be sent directly to the Web browser, so you can even include HTML tags in your output if you wish, simply by including them inside the quotes.

The ability to print the value of a variable inside other text is called string interpolation. That's not critical to know, but it could be useful information on a trivia show or something.

Using the Semicolon to End a Line

If you look back at the complete code for the hiJacob program, you can see that it has two lines of code inside the PHP block. Each line of PHP code ends with a semicolon. PHP is a more formal language than HTML and, like most programming languages, has some strict rules about the syntax used when writing a page.

Each unique instruction is expected to end with a semicolon. You'll end most lines of PHP code with a semicolon. If you forget to do this, you'll get an error that looks like Figure 2.4.

Click To expand
Figure 2.4: This error will occur if you forget to add a semicolon to the end of every line.

If you see this particular message, look back at your code to ensure you've remembered to add a semicolon at the end of the line.


There will be times when an instruction is longer than a single line on the editor. The semicolon goes at the end of the instruction, which often (but not always) corresponds with the end of the line.


Don't panic if you get an error message or two. They are a completely normal part of programming. Even experienced programmers expect to see many error messages while building and testing programs. Usually the resulting error code gives you important clues about what went wrong. Make sure you look carefully at whatever line of code the error message reports. Although the error isn't always on that line, you can often get a hint what went wrong by examining that line closely. In many cases (particularly a missing semicolon), a syntax error will indicate an error on the line that actually follows the real problem. If you get a syntax error on line 14, and the problem is a missing semicolon, the problem line is actually line 13.