Specifications

FIGURE 2.3
Using your own error messages instead of PHPs can be more user friendly.
Writing to a File
Writing to a file in PHP is relatively simple. You can use either of the functions fwrite() (file
write) or fputs() (file put string); fputs() is an alias to fwrite(). We call fwrite() in the
following:
fwrite($fp, $outputstring);
This tells PHP to write the string stored in $outputstring to the file pointed to by $fp. Well
discuss fwrite() in more detail before we talk about the contents of $outputstring.
Parameters for fwrite()
The function fwrite() actually takes three parameters but the third one is optional. The proto-
type for
fwrite() is
int fputs(int fp, string str, int [length]);
The third parameter, length, is the maximum number of bytes to write. If this parameter is
supplied, fwrite() will write string to the file pointed to by fp until it reaches the end of
string or has written length bytes, whichever comes first.
Storing and Retrieving Data
C
HAPTER 2
2
STORING AND
RETRIEVING DATA
57
04 7842 CH02 3/6/01 3:37 PM Page 57