Chapter 2 Storing and Retrieving Data Flashcards

1
Q

Define and Show: fopen()

A

Opens a file.

$fp = fopen(“$DOCUMENT_ROOT/bobs/orders.txt, ‘ab’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

fopen file mode: r

A

Read. Open the file for reading, beginning from the star of the file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

fopen file mode: r+

A

Read. Open the file for reading and writing, beginning from the start of the file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

fopen file mode: w

A

Write. Open the file for writing, beginning from the start of the file. If the file exists, delete the existing contents. If it does not exist, try to create it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

fopen file mode: w+

A

Write. Open the file for writing and reading, beginning from the start of the file. If the file already exists, delete the existing contents. If it does not exist, try to create it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

fopen file mode: x

A

Cautious write. Open the file for writing, beginning from the start of the file. If the file already exists, it will not be opened, fopen() will return false, and PHP will generate a warning.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

fopen file mode: x+

A

Cautious write. Open the file for writing and reading, beginning from the start of the file. If the file already exists, it will not be opened, fopen() will return false, and PHP will generate a warning.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

fopen file mode: a

A

Append. Open the file for appending (writing) only, starting from the end of the existing contents, if any. If it does not exist, try to create it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

fopen file mode: a+

A

Append. Open the file for appending (writing) and reading, starting from the end of the existing contents, if any. If it does not exist, try to create it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

fopen file mode: b

A

Binary. Used in conjunction with one of the other modes. You might want to use this mode if your file system differentiates between binary and text file. Windows systems differentiate; Unix systems do not. The PHP developers recommend you always us this option for maximum portability. It is the default mode.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

fopen file mode: t

A

Text. Used in conjunction with one of the other modes. This mode is an option only in Windows systems. It is not recommended except before you have ported your code to work with the b option.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Define and Show: fwrite() or puts()

A

Write to a file.

fwrite($fp, $outputstring);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Define and Show: @

A

Error suppression operator is used to suppress Wordpresses default error message.

@ $fp = fopen(“$DOCUMENT_ROOT/bobs/orders.txt, ‘ab’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Define and Show: file_put_contents()

A

This function writes the string to the file without any need for an fopen() function call.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Define and Show: fclose()

A

Close a file.

fclose($fp);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

\n

A

New line.

17
Q

\t

A

Tab.

18
Q

Define and Show: feof()

A

File end of file. The feof() function takes a file handle as its single parameter. It returns true if the file pointer is at the end of the file.

while (!feof($fp)) {…

19
Q

Define and Show: fgets()

A

This function reads one line at a time from a file until it has read 998 bytes from the file.

$order = fgets($fp, 999);

20
Q

Define and Show: fgetss()

A

This function is similar to fgets() except that it strips out any PHP and HTML tags found in the string.

$order = fgets($fp, 999);

21
Q

Define and Show: fgetscsv()

A

This function breaks up lines when you have used a delimiting character, such as the tab character or a comma.

$order = fgetcsv($fp, 100, “\t”);

22
Q

Define and Show: readfile()

A

First option to read from a file. This function opens the file, echoes the content to the browser and then closes the file all in one go.

readfile(“$DOCUMENT_ROOT/bobs/orders.text”);

23
Q

Define and Show: fpassthru()

A

Second option to read from a file. First open a file using fopen(). Then pass the file pointer as an argument to fpassthru(), which dumps the contents of the file from the pointer’s position onward to the browser. It closes the file when it is finished.

$fp = fopen(“$DOCUMENT_ROOT/bobs/orders.txt”, ‘rb’);
fpassthru($fp);

24
Q

Define and Show: file()

A

Third option to read from a file. This function is identical to readfile() except that instead of echoing the file to the browser, it turns it into an array.

$filearray = file(“$DOCUMENT_ROOT/bobs/orders.txt”);

25
Q

Define and Show: file_get_contents()

A

Fourth option to read from a file. This function is identical to readfile() except that it returns the content of the file as a string instead of outputting it to the browser.

$mystring = file_get_contents(“$DOCUMENT_ROOT/bobs/orders.text”);

26
Q

Define and Show: fgetsc()

A

Reads a single character at a time from a file. It takes a file pointer as its only parameter and returns the next character in the file.

while(!feof($fp)) {
     $char = fgetc($fp);
     if (!feof($fp)) {
          echo $char == "\n" ? "<br />" : $char);
     }
}
27
Q

Define and Show: fread()

A

Reads an arbitrary number of bytes from a file. It reads up to length bytes, to the end of the file or network packet, whichever comes first.

$contents = fread($fp, 999);

28
Q

Define and Show: file_exists()

A

Checks to see if a file exists without actually opening it.

if (file_exists($DOCUMENT_ROOT/bobs/orders.txt)) {
echo ‘There are orders waiting to be processed.’;
} else {
echo ‘There are currently no orders’;
}

29
Q

Define and Show: filesize()

A

Checks the size of a file in bytes.

echo filesize(“$DOCUMENT_ROOT/bobs/orders.txt”);

30
Q

Define and Show: nl2br()

A

Converts the \n (new line) characters in the output to HTML line breaks (<br></br>).

echo nl2br(fread($fp, filesize($DOCUMENT_ROOT/bobs/orders.txt”)));

31
Q

Define and Show: unlink()

A

Used to delete the order file after the orders have been processed. There is no delete function.

unlink(“$DOCUMENT_ROOT/bobs/orders.txt”);

32
Q

Define and Show: rewind()

A

This function resets the file pointer to the beginning of the file.

rewind($fp);

33
Q

Define and Show: ftell()

A

This function reports how far into the file the pointer is in bytes.

echo ‘Final position of the file pointer is ‘.ftell($fp);

34
Q

Define and Show: fseek()

A

Sets the file pointer fp at the starting point from whence and moving offset bytes into the file. The optional whence parameter defaults to the value SEEK_SET, which is the start of the file. The other possible values are SEEK_CUR (the current location of the file pointer) and SEEK_END (the end of the file).

fseek($fp, 0); // start of the file.

35
Q

Define and Show: flock()

A

Locks a file. You need to pass it a pointer to an open file and a constant representing the kind of lock you require. It returns true if the lock was successfully acquired and false if it was not. You need to add it to all the scripts that use the file otherwise it is worthless.

$fp = fopen(“$DOCUMENT_ROOT/../orders/orders.txt”, ‘ab’); flock($fp, LOCK_EX); // lock the file for writing fwrite($fp, $outputstring);
flock($fp, LOCK_UN); // release write lock
fclose($fp);

36
Q

flock() parameters: LOCK_SH

A

Reading lock. The file can be shared with other readers.

37
Q

flock() parameters: LOCK_EX

A

Writing lock. This operation is exclusive; the file cannot be shared.

38
Q

flock() parameters: LOCK_UN

A

The existing lock is released.

39
Q

flock() parameters: LOCK_NB

A

Blocking is prevented while you are trying to acquire a lock.