PHP feof () function instance test, phpfeof

Source: Internet
Author: User

PHP feof () function instance test, 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 <EOF <! DOCTYPE html> 

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 )){}

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.