PHP feof () function instance test, phpfeof_PHP tutorial

Source: Internet
Author: User
PHP feof () function instance test, phpfeof. PHP feof () function instance test. phpfeof this article describes the usage of feof () function in PHP, and tests feof () function to some extent, which is of great practical value. The specific analysis is as follows: feof () function instance test in PHP, phpfeof

This article describes the usage of the feof () function in PHP and tests the feof () function. it is of great practical value. The specific analysis is as follows:

The running environment of the instance in this article:

OS: Mac OS X 10.8.4
PHP: 5.3.15

In the official PHP manual, the feof () function has a lot of discussions, and some related tests are carried out as follows.

The test code is as follows:

<? Phpprint <
       
      
  Test the feof () function in PHP.        
  

EOF; function bool2str ($ bool) {if ($ bool = TRUE) {return "TRUE" ;}else {return "FALSE ";}} /** create a file at will. * For example, in this test, a text file is created under the same path of the script file. * The file content is "abcdefg" and the file name is "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 ). ":
\ N "; echo" fseek is executed. before the read operation is executed, feof result: ". bool2str (feof ($ handle ))."
\ N "; echo" current position character: ". fgetc ($ handle )."
\ N "; echo" after the file is read, feof result: ". bool2str (feof ($ handle ))."

\ N ";}/** you can see through the above code that * with the execution of the loop, the file pointer is always moved from the file header to the end of the file. * When the read output of the character "g" is completed, the file pointer moves backward. this is feof () and False is returned. * True is returned only after a fgetc () operation is performed, indicating that the object is reached the end of the object. */Echo "ftell () result:". ftell ($ handle )." \ N "; // output, it is depressing to find that the position of the file pointer is still 7. + _ + Fseek ($ handle, 4); echo "file location". ftell ($ handle ).":
\ N "; echo" fseek is executed. before the read operation is executed, feof result: ". bool2str (feof ($ handle ))."
\ N "; echo" current position character: ". fgetc ($ handle )."
\ N "; echo" after the file is read, feof result: ". bool2str (feof ($ handle ))." \ N "; fseek ($ handle, 7); echo" file location ". ftell ($ handle ).":
\ N "; echo" fseek is executed. before the read operation is executed, feof result: ". bool2str (feof ($ handle ))."
\ N "; echo" current position character: ". fgetc ($ handle )."
\ N "; echo" after the file is read, feof result: ". bool2str (feof ($ handle ))." \ N "; fclose ($ handle); // move the file pointer again, and the effect remains unchanged. // Use another code to test: $ 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); // The loop is terminated only after the character g is output and the read operation is executed again. Print < EOF;?>

In PHP, the feof () implementation method does not directly check the position of the file pointer relative to the file, but returns the result based on a certain identifier. This identifier is set to "False" after each fseek () operation. the identifier is set only after a file content read operation is executed.

Based on this assumption, two types of code logic can be used.

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

The sample code is as follows:

While ($ content = fgets ($ fileHandle ))! = FALSE) {// file content processing ...... }

This solution utilizes the PHP-criticized function return method, so you must use "=" or "! = "For detection, the code cannot be simplified:

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

Another method is to read the file one time before entering the feof () loop, as shown below:

$ Content = fgets ($ fileHandle); while (! Feof ($ fileHandle) {// process file content ...... $ Content = fgets ($ fileHandle );}

After testing, the previous method is more efficient.

I hope this example will help you with PHP programming.


What is the feof () function in php?

The feof (fp) function checks whether it has reached the end of the file (eof ).
There are two return values: If the file ends, the feof (fp) value of the function is a non-zero value; otherwise, it is 0.

Feof () function in PHP

Let me talk about feof () first. This function is used to determine whether the file ends ,! Feof () means the file is not over yet. If you use the if statement, the result is that if the file is not finished, it is normal to run the following statement to get 12342, because this condition is only executed once as required. If you want to get your results, you must let it judge cyclically and add a loop statement to it! While (! Feof ($ file )){}

Example test of lift () function. phpfeof this article describes the usage of feof () function in PHP. it is of great practical value to test feof () function. The specific analysis is as follows :...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.