Specifications

File Formats
When you are creating a data file like the one in our example, the format in which you store
the data is completely up to you. (However, if you are planning to use the data file in another
application, you may have to follow that applications rules.)
Lets construct a string that represents one record in our data file. We can do this as follows:
$outputstring = $date.”\t”.$tireqty.” tires \t”.$oilqty.” oil\t”
.$sparkqty.” spark plugs\t\$”.$total
.”\t”. $address.”\n”;
In our simple example, we are storing each order record on a separate line in the file. We choose
to write one record per line because this gives us a simple record separator in the newline charac-
ter. Because newlines are invisible, we represent them with the control sequence “\n”.
We will write the data fields in the same order every time and separate fields with a tab charac-
ter. Again, because a tab character is invisible, it is represented by the control sequence “\t”.
You may choose any sensible delimiter that is easy to read back.
The separator, or delimiter, character should either be something that will certainly not occur in
the input, or we should process the input to remove or escape out any instances of the delimiter.
We will look at processing the input in Chapter 4, String Manipulation and Regular
Expressions.For now, we will assume that nobody will place a tab into our order form. It is dif-
ficult, but not impossible, for a user to put a tab or newline into a single line HTML input field.
Using a special field separator will allow us to split the data back into separate variables more
easily when we read the data back. Well cover this in Chapter 3, Using Arrays,and Chapter
4. For the time being, well treat each order as a single string.
After processing a few orders, the contents of the file will look something like the example
shown in Listing 2.1.
LISTING 2.1 orders.txtExample of What the Orders File Might Contain
15:42, 20th April 4 tires 1 oil 6 spark plugs $434.00
22 Short St, Smalltown
15:43, 20th April 1 tires 0 oil 0 spark plugs $100.00
33 Main Rd, Newtown
15:43, 20th April 0 tires 1 oil 4 spark plugs $26.00
127 Acacia St, Springfield
Closing a File
When youve finished using a file, you need to close it. You should do this with the fclose()
function as follows:
fclose($fp);
Using PHP
P
ART I
58
04 7842 CH02 3/6/01 3:37 PM Page 58