Feof () function instance test in PHP, phpfeof
In this paper, the use of the feof () function in PHP is described, and it is useful to test the feof () function for some practical applications. 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 given below.
The test code is as follows:
<?phpprint <<
<title>Test the feof () function effect in PHP</title> Eof;function Bool2str ($bool) {if ($bool = = True) {return ' true '; } else {return ' FALSE '; }}/* * Please feel free to create a file. * For example: In this test, a text file is created under the same path as the script file, * The file content is "ABCDEFG" and the file name is "7bytesfile". */$filename = './7bytesfile '; $handle = fopen ($filename, ' R '), if (! $handle) {die ("File open Failed");} for ($i = 0; $i <= filesize ($filename); $i + +) {fseek ($handle, $i); echo "File location". Ftell ($handle). ":
\ n "; echo "feof The result before performing a read operation fseek:". Bool2str (feof ($handle)). "
\ n "; echo "Current position character:". Fgetc ($handle). "
\ n "; echo "After performing a file read operation, feof results:". Bool2str (feof ($handle)). "
\ n ";} /* * As shown in the previous section of the code, * as the loop executes, the file pointer moves from the file header to the end of the file. * But when the read output of the character "G" is completed, the file pointer continues to move backwards, which is feof () still returns false. * Returns true only after a fgetc () operation has been performed, indicating that the end of the file is reached. */echo "Ftell () Result:". Ftell ($handle). "
\ n ";//output, very depressed find the location of the file pointer is still 7. +_+fseek ($handle, 4); echo "File location". Ftell ($handle). ":
\ n "; echo" executes fseek, before the read operation has been performed, feof the result: ". Bool2str (feof ($handle)). "
\ n "; echo" Current position character: ". Fgetc ($handle). "
\ n "; echo" after performing a file read operation, feof the result: ". Bool2str (feof ($handle)). "
\ n "; fseek ($handle, 7); echo" File location ". Ftell ($handle). ":
\ n "; echo" executes fseek, before the read operation has been performed, feof the result: ". Bool2str (feof ($handle)). "
\ n "; echo" Current position character: ". Fgetc ($handle). "
\ n "; echo" after performing a file read operation, feof the result: ". Bool2str (feof ($handle)). "
\ n "; fclose ($handle);//move the file pointer again, the effect is still. Test it with another piece of code: $handle = fopen ($filename, ' R '), if (! $handle) {die ("File open Failed");} while (!feof ($handle)) {$char = fgetc ($handle); if ($char = = = False) {echo ' false '; } else {echo $char; }}fclose ($handle);//After the output of the character G, the read operation is performed again before terminating the loop. Print <<
EOF;?
< body=""> >
The speculation in this case is that, in PHP, the implementation of feof () does not directly check the location of the file pointer relative to the file, but instead returns the result based on an identity. This flag is set to "False" after each fseek (), and the identity is set only after the file read operation is performed, based on the results of the file reading.
Based on this speculation, two kinds of code logic can be used.
One method is to not do feof () detection and directly detect the execution result of the content reading function (such as fgetc (), fgets ()).
The sample code is as follows:
while (($content = fgets ($fileHandle))!==false) { //file content processing ...}
This approach, using PHP is criticized by the function return method, so you have to use "= = =" or "!==" to detect, can not simplify the code into:
while ($content = fgets ($fileHandle)) {}
Another way is to do a file read first, 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 the example of this article will help you with PHP programming.
What is the feof () function in PHP?
The feof (FP) function detects if the end of file (EOF) has been reached.
There are two return values: If the end of the file is encountered, the value of the function feof (FP) is a value other than 0, otherwise 0.
Feof () function in PHP
First tell you feof (), this function is to determine whether the file is finished,!feof () means the file is not finished. If you use the IF statement, the result is that if the file does not end, execute the following statement, get 12342, it is normal, because this condition according to your request, also only executes once. If you want to get your results, you have to let it cycle judgment, and give it a loop statement! while (!feof ($file)) {}
http://www.bkjia.com/PHPjc/868230.html www.bkjia.com true http://www.bkjia.com/PHPjc/868230.html techarticle php feof () function instance test, phpfeof This example of the feof () function in PHP, the use of the feof () function for a certain test, it is very practical value. The specific analysis is as follows: ...