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 in 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. 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 hiJacob.php source code. <html> <head> <title>Hi Jacob</title> </head> <body> <h1>Hi Jacob</h1> 23 C h a p t e r 2 U s i n g V a r i a b l e s a n d I n p u t FIGURE 2.2 I hate it when the warthog’s in the kohlrabi. <h3>Demonstrates using a variable</h3> <? $userName = “Jacob”; print “Hi, $userName”; ?> </body> </html> In regular HTML and JavaScript programming, you can use the Web browser’s view source command to examine your program code. For server-side lan- guages, this is not sufficient; the view source document has no PHP at all. Remember that the actual program code never gets to your Web browser. Instead, the program is executed on the server and the program results are sent to the browser as ordinary HTML. Be looking at the actual PHP source code on the server when examining these programs. On a related note, you cannot simply use your browser’s File menu to load a PHP page. Instead, run it through a server. TRAP 24 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r FIGURE 2.3 The word Jacob is stored in a variable in this page. The helloJacob 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 two major things. First, it creates a variable named $userName. Second, it will assign the value “Jacob” to the variable. In PHP, all variables begin with a dollar sign to distinguish them from other program ele- ments. The variable’s name is significant. Naming Your Variables As a programmer, you 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 symbols. Most of the special characters such as #, *, and / already have meaning in programming languages. Of course, every vari- able 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. PHP will not catch a misspelling. Instead, it makes a whole new variable, and your program probably won’t work correctly. 25 C h a p t e r 2 U s i n g V a r i a b l e s a n d I n p u t 26 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r Assigning a Value to a Variable The equals sign (=) is special in PHP. It does not mean equals (at least in the pre- sent context). The equals sign is used for assignment. If you read the equals sign as the word gets, you are closer to the meaning PHP uses for this symbol. For example, look at this line of code: $userName = “Jacob” It should be read as The variable $userName gets the value Jacob. Usually when you create a variable in PHP, you also assign 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 by determining the data type of a variable based on its context. 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 a Variable’s Value 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 quotation marks. If you wish to print the value of a variable, simply place the variable name in the text you want printed. Examine the following line: print “Hi, $userName”; It actually produces this output: Hi, Jacob It produces this because when the server encounters the variable $userName, it’s replaced with that variable’s value, which is Jacob. The PHP program output is sent directly to the Web browser, so you can even include HTML tags in your out- put, simply by including them inside the quotation marks. The ability to print the value of a variable inside other text is called string inter- polation. That’s not critical to know, but it could be useful information on a trivia show or something. TRICK TRICK 27 C h a p t e r 2 U s i n g V a r i a b l e s a n d I n p u t Using the Semicolon to End a Line PHP is a more formal language than HTML and, like most programming lan- guages, has some strict rules about the syntax used when writing. Each unique instruction is expected to end with a semicolon. If you look at the complete code for the helloJacob program, you see that each line of PHP code ends with said semicolon. If you forget to do this, you get an error that looks like the one in Figure 2.4. An instruction sometimes 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. You’ll see an example of this shortly as you build long string variables. In general, though, you end each line with a semicolon. Don’t panic if you get an error message or two. Errors are a completely normal part of programming. Even experienced programmers expect to see many error mes- sages 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 is reported. Although the error isn’t always on that line, you can often get a hint. In many cases (particularly a missing semicolon), a syntax error indicates 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. HINT HINT FIGURE 2.4 This error occurs if you go sans semicolon to the end of every line. . 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. HTML. Be looking at the actual PHP source code on the server when examining these programs. On a related note, you cannot simply use your browser’s File menu to load a PHP page. Instead, run it through. 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. PHP will not catch