This article illustrates the use of the feof () function in PHP, and tests the feof () function for some practical value. The specific analysis is as follows:
This example runs the environment:
Os:mac OS X 10.8.4
php:5.3.15
In the official PHP manual, the function feof () is discussed below, and some of the relevant tests are as follows.
The test code is as follows:
<?php Print <<<eof <! DOCTYPE html>
The guess in this case is that in PHP, feof () is not implemented by directly checking the location of the file pointer relative to the file, but by returning the result based on an identity. The identity is set to "False" after each fseek (), and the identity is set only after a file-content read operation is performed based on the results read by the file.
Based on this guess, you can use two kinds of code logic.
One method is to not do feof () detection, directly detect the content read functions (such as fgetc (), fgets ()) the execution results.
The sample code is as follows:
while ($content = fgets ($fileHandle))!==false) {
//file content processing ...
}
This approach, using the PHP is criticized for the return of the function, so you have to use "= = =" or "!==" to detect, can not simplify the code into:
while ($content = fgets ($fileHandle)) {}
Another method is to do a file read first and then into the feof () loop , as follows:
$content = fgets ($fileHandle);
while (!feof ($fileHandle)) {
//process file contents
... $content = fgets ($fileHandle);
}
After testing, the former method is more efficient.
I hope this example will help you with your PHP programming.