Chapter 6: Writing and Editing |
|
Initializing variables |
|
Suppose you wanted your script to count something starting with 0. You'd have a variable such as $count, and you'd assign it a value of zero at the beginning of the script like this: $count = 0; Since you gave it an "initial" value, you have "initialized" it. Easy, right? Sure it is. Now what if you want your script to know about a group of items like a list of colors, for example? You'd initialize an array. It could be done like this: $colors = array("red", "green", "blue", "purple", "orange"); While the program is running, you can refer to "red" as $colors[0], "blue" as $colors[2] and "orange" as $colors[4]. The number in [ ]s is called a subscript; it tells the program where to look in the list for an item. This is how arrays are handled in almost every computer language. PHP is more flexible with arrays than most. A single item from a list starts with "$" so that your program can tell which things are items and which things are lists. Since the "items" in the list are scalars and scalars can be numeric or string data, a list can be all mixed up: |
|
|
$mylist = array(1, "five", 3.14, "Bob", "email", 42); But wait! There's more. Even though each item in the list is considered to be a single item when you retrieve it from the list, that item could be a whole separate list. So $lists could be a list of lists. For each item(list) in the list of lists, all the rules for a list will apply. PHP takes all this a step further than some other languages, which lets you do really handy things. It has another array type called an "associative" array. The subscripts in this array can be anything you want them to be! How cool is that? Here's an example: $fruit = array("pears => 2, "lemons" => "sour", "grapes" => 100); These arrays also use [ ] to indicate the subscript. So now, $fruit[pears] means 2, $fruit[lemons] means sour and $fruit[grapes] means 100. Notice that when you defined the associative array, you listed a subscript, then a value, then another subscript, another value, etc. That's important to remember! |
Comments: documenting your code |
Any line that begins with a "#" is a comment. The command processor will ignore these lines. This works in PHP and many shell-scripting languages. Other languages such as C++ use // or /*...*/ to indicate a comment. This can be used to your advantage in two important ways. One, you can add plain language that explains what a certain part of the code is supposed to be doing. If you document your code as you write it, it will be a lot easier to make sense of it later on! Two, you can temporarily put the comment character in front of lines of real code that you don't want to run. Now, why would you want to do that? It's a way of debugging the code. See the section on Debugging for a more complete explanation. |
|
|
The main body |
|
OK, now we've got things set up so we can run our script and give it some initial data. The main body is where we put code that only needs to run once (or needs to run in a very specific order). It's also the place where the action starts and (usually) ends. Peek ahead to the end of this chapter. There's a section called "Anatomy of a simple PHP script". This script only has a main body; there are no functions and no calls to anything outside the main body. Only a very small script is ever likely to look that way. What is more often done is to make the main body little more than a bunch of calls to functions. There will usually be some decision (if) statements here and there. They act like traffic cops, directing the flow of the script. They follow this pattern: if (this condition is true) { Depending on how the decision must be made, there can be different numbers of clauses. There might be just an "if" statement, an "if" with an "else" or an "if" with one or more "elseif" clauses and then, finally, an "else" clause. At the very end is a "die" statement. This signals the server that the script has finished. In the main body, you don't always need it but there's no harm in having one. This statement consists of the word "die" followed by a semicolon. An "error message" is optional but, when used, it's enclosed in parentheses and placed between the word "die" and the semicolon. |
| Previous Page Table of Contents Next Page |
Copyright © 2004 Steve Humphrey |