↓ This tag tells PHP to interpret what follows as PHP code.
↓ This is a call to the header() function which is used to set HTTP response headers. The Content-Type header tells the browser what kind of document it should expect to receive. The text/plain MIME type means that the document will be plain text. Usually, the content type defaults to text/html; but I didn't want to deal with escaping HTML that might be in the data file; and I didn't want to use HTML tags like <br> or <pre> to force line endings to be visible in the browser.
Code: Select all
header('Content-Type: text/plain');
↓ This statement initializes a variable named $output to hold an empty string. The string will act as a buffer to collect file data.
↓ Here, the fopen() function is called to create a file resource which is assigned to the variable $handle. The file opened is "data.txt". The filename has no directory path before it, so it is probably located in the same directory as the PHP script. The second argument passed to fopen(), "r", causes the file to be opened in read mode.
↓ This conditional statement ensures that fopen() actually returned a resource rather than FALSE (a boolean).
↓ This statement is a little more complex. First, the function fgets() is called to retrieve a line from the data file. Then, the returned value is assigned to the $line variable. Finally, the value of the assignment expression (which happens to be equal to the value stored in $line) is evaluated as a boolean that tells the while loop whether to process the statements in its block (the statements between the curly braces). Because most strings become TRUE when cast as boolean, the while loop will continue as long as fgets() returns a string. (There is a problem with this statement. See the note at the end of my post.*) When fgets() runs out of lines to read, it returns FALSE and the loop terminates.
↓ This statement concatenates (joins) the $line string with the $output string and assigns the result to the $output variable. Because subsequent lines are placed at the beginning of the buffer, the lines will later be displayed in reverse order.
↓ This curly bracket marks the end of the while block.
↓ Calling fclose() releases the file resource. PHP would close it automatically with its garbage cleanup after the script ends, but it is a good practice to free resources as soon as possible.
↓ This curly bracket marks the end of the if block.
↓ Finally, the string containing the buffered lines is echoed out.
*Note: There is a problem with the while loop condition. As I said, most strings become TRUE when cast as boolean. However, "0" becomes FALSE. This is not a problem for most lines because fgets() returns the line ending; so the line "0" is actually "0\n" or "0\r\n" depending on the convention used by the program that last saved the file. Such strings become TRUE when cast as boolean. The problem is that the last line in a file might not have a line ending. In that case, fgets() returns "0" and that line will be ignored.
↓ Here, the value returned by fgets() must be
identical (not just
equal) to FALSE.
Code: Select all
while (($line = fgets($handle)) !== false) {