Example test of feof of function in PHP

  • 2021-07-18 07:14:10
  • OfStack

In this paper, the usage of feof () function in PHP is described by examples, and the test of feof () function is carried out, which is of great practical value. The specific analysis is as follows:

The running environment of this article example is:

OS: Mac OS X 10.8.4
PHP: 5.3. 15

In the official manual of PHP, the function feof () is discussed a lot below, and some related tests are done as follows.

The test code is as follows:


<?php
print <<<EOF
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title> Test PHP In feof() Function effect </title>
  </head>
  <body>
    <div>
EOF;
function bool2str($bool) {
  if ($bool == TRUE) {
    return "TRUE";
  } else {
    return "FALSE";
  }
}
/*
 *  Please feel free to create 1 File. 
 *  For example, in this test, under the same path as the script file, the 1 Text files, 
 *  The content of the file is " abcdefg "With the file name" 7bytesfile ". 
 */
$filename = './7bytesfile';
$handle = fopen($filename, 'r');
if (!$handle) {
  die(" File opening failed ");
}
for($i = 0; $i <= filesize($filename); $i++) {
  fseek($handle, $i);
  echo " File location " . ftell($handle) . " : <br />\n";
  echo " Execute fseek Before the read operation has been performed, feof Results: " . bool2str(feof($handle)) . "<br />\n";
  echo " Current position character: " . fgetc($handle) . "<br />\n";
  echo " After the file read operation is performed, feof Results: " . bool2str(feof($handle)) . "<hr />\n";
}
/*
 *  Through the top 1 Section code can be observed, 
 *  As the loop executes, the file pointer starts from the file header 1 Move straight to the end of the file. 
 *  But when the characters are finished, " g ", the file pointer continues to move backward, which is feof() Still return False . 
 *  Only when the 1 Times fgetc() Operation before returning true That means the end of the file is reached. 
 */
echo "ftell() Results: ". ftell($handle). "<hr />\n";
// Output 1 I am very depressed to find that the position of the file pointer is still 7 . +_+

fseek($handle, 4);
echo " File location " . ftell($handle) . " : <br />\n";
echo " Execute fseek Before the read operation has been performed, feof Results: " . bool2str(feof($handle)) . "<br />\n";
echo " Current position character: " . fgetc($handle) . "<br />\n";
echo " After the file read operation is performed, feof Results: " . bool2str(feof($handle)) . "<hr />\n";

fseek($handle, 7);
echo " File location " . ftell($handle) . " : <br />\n";
echo " Execute fseek Before the read operation has been performed, feof Results: " . bool2str(feof($handle)) . "<br />\n";
echo " Current position character: " . fgetc($handle) . "<br />\n";
echo " After the file read operation is performed, feof Results: " . bool2str(feof($handle)) . "<hr />\n";
fclose($handle);
// Move the file pointer again, and the effect remains the same. 
// And then use another 1 Segment code test 1 Below: 

$handle = fopen($filename, 'r');
if (!$handle) {
  die(" File opening failed ");
}
while (!feof($handle)) {
  $char = fgetc($handle);
  if ($char === FALSE) {
    echo 'FALSE';
  } else {
    echo $char;
  }
}
fclose($handle);
// Characters are still output g After that, the read operation is performed again before the loop is terminated. 

print <<<EOF
    </div>
  </body>
</html>
EOF;
?>

The guess for this scenario is that in PHP, feof () is implemented in such a way that instead of directly checking the position of the file pointer relative to the file, it returns the result based on an identity. This identity will be set to "False" after every fseek (), and the identity will be set according to the result of file reading only after the file content reading operation is performed once.

According to this guess, two kinds of code logic can be used.

One method is to directly detect the execution results of content reading functions (such as fgetc () and fgets ()) without feof () detection.

The sample code is as follows:


while (($content = fgets($fileHandle)) !==FALSE) {
   // File content processing...  
}

This kind of processing method makes use of the function return method criticized by PHP, so it has to be detected with "= = =" or "! = =", and the code cannot be simplified as:


while ($content = fgets($fileHandle)) {}

The other method is to read the file once before entering the feof () loop, as follows:


$content = fgets($fileHandle);
while (!feof($fileHandle)) {
  // Handle the contents of the file … 
  $content = fgets($fileHandle); 
}

After testing, the efficiency of the first one method will be 1 higher.

I hope this example is helpful to everyone's PHP programming.


Related articles: