Test the feof () function instance in PHP

Source: Internet
Author: User
This article mainly introduces the feof () function instance test in PHP. if you need it, you can refer to the example in this article to describe the usage of the feof () function in PHP () function tests are very useful. 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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.