Home > Backend Development > PHP Tutorial > Example test of feof() function in PHP, phpfeof_PHP tutorial

Example test of feof() function in PHP, phpfeof_PHP tutorial

WBOY
Release: 2016-07-13 10:20:14
Original
1006 people have browsed it

Example test of feof() function in PHP, phpfeof

The example in this article describes the usage of the feof() function in PHP. It has conducted certain tests on the feof() function, which is of great practical value. The specific analysis is as follows:

The example running environment of this article:

OS: Mac OS X 10.8.4
PHP: 5.3.15

In the official PHP manual, there is a lot of discussion about the function feof(). Some related tests have been done as follows.

The test code is as follows:

<&#63;php
print <<<EOF
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>测试PHP中的feof()函数效果</title>
  </head>
  <body>
    <div>
EOF;
function bool2str($bool) {
  if ($bool == TRUE) {
    return "TRUE";
  } else {
    return "FALSE";
  }
}
/*
 * 请随便创建一个文件。
 * 比如:本测试中,在脚本文件的相同路径下创建了一个文本文件,
 * 文件内容为“abcdefg”,文件名为“7bytesfile”。
 */
$filename = './7bytesfile';
$handle = fopen($filename, 'r');
if (!$handle) {
  die("文件打开失败");
}
for($i = 0; $i <= filesize($filename); $i++) {
  fseek($handle, $i);
  echo "文件位置" . ftell($handle) . ":<br />\n";
  echo "执行fseek,尚未执行读取操作之前,feof结果:" . bool2str(feof($handle)) . "<br />\n";
  echo "当前位置字符:" . fgetc($handle) . "<br />\n";
  echo "执行文件读取操作之后,feof结果:" . bool2str(feof($handle)) . "<hr />\n";
}
/*
 * 通过上面一段代码可以观察到,
 * 随着循环的执行,文件指针从文件头一直移动到文件末尾。
 * 但是当完成了字符“g”的读取输出,文件指针继续向后移动,这是feof()依然返回False。
 * 只有当执行了一次fgetc()操作之后,才返回true,表示到达文件末尾。
 */
echo "ftell()结果:". ftell($handle). "<hr />\n";
//输出一下,很郁闷的发现文件指针的位置还是7。+_+

fseek($handle, 4);
echo "文件位置" . ftell($handle) . ":<br />\n";
echo "执行fseek,尚未执行读取操作之前,feof结果:" . bool2str(feof($handle)) . "<br />\n";
echo "当前位置字符:" . fgetc($handle) . "<br />\n";
echo "执行文件读取操作之后,feof结果:" . bool2str(feof($handle)) . "<hr />\n";

fseek($handle, 7);
echo "文件位置" . ftell($handle) . ":<br />\n";
echo "执行fseek,尚未执行读取操作之前,feof结果:" . bool2str(feof($handle)) . "<br />\n";
echo "当前位置字符:" . fgetc($handle) . "<br />\n";
echo "执行文件读取操作之后,feof结果:" . bool2str(feof($handle)) . "<hr />\n";
fclose($handle);
//再次移动文件指针,效果依旧。
//再用另外一段代码测试一下:

$handle = fopen($filename, 'r');
if (!$handle) {
  die("文件打开失败");
}
while (!feof($handle)) {
  $char = fgetc($handle);
  if ($char === FALSE) {
    echo 'FALSE';
  } else {
    echo $char;
  }
}
fclose($handle);
//依然是输出了字符g之后,再次执行读取操作,才终止循环。

print <<<EOF
    </div>
  </body>
</html>
EOF;
&#63;>

Copy after login

The guess for this situation is that in PHP, the implementation of feof() does not directly check the position of the file pointer relative to the file, but returns the result based on a certain identifier. This flag will be set to "False" after each fseek(). Only after a file content reading operation is performed, the flag will be set based on the result of the file reading.

Based on this guess, two code logics can be used.

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

The sample code is as follows:

while (($content = fgets($fileHandle)) !==FALSE) {
   //文件内容处理…… 
}

Copy after login

This method of processing takes advantage of PHP’s criticized function return method, so it must use “===” or “!==” for detection. The code cannot be simplified to:

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

Copy after login

Another method is to read the file first and then enter the feof() loop , as shown below:

$content = fgets($fileHandle);
while (!feof($fileHandle)) {
  //处理文件内容……
  $content = fgets($fileHandle); 
}

Copy after login

After testing, the former method is more efficient.

I hope the examples in this article will be helpful to everyone in PHP programming.

What is the feof() function in php?

The feof(fp) function detects whether the end of file (eof) has been reached.
There are two return values: if the end of file is encountered, the value of function feof(fp) is non-zero, otherwise it is 0.

feof() function in PHP

Let me tell you about feof() first. This function is to determine whether the file has ended. !feof() means that the file has not ended yet. If you use the if statement, the result you get is that if the file is not ended, execute the following statement and get 12342, which is normal, because this condition is only executed once according to your requirements. If you want to get your result, you must make it loop and judge. Add a loop statement to it! while(!feof($file)){}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/868230.htmlTechArticleExample test of feof() function in PHP, phpfeof This article describes the usage of feof() function in PHP, Certain tests have been conducted on the feof() function, which is of great practical value. The specific analysis is as follows:...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template