PHP feof () Function instance test _php technique

Source: Internet
Author: User
Tags php print php programming

This article illustrates the use of the feof () function in PHP, and tests the feof () function for some practical value. 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 as follows.

The test code is as follows:

<?php Print <<<eof <! DOCTYPE html>  

The guess in this case is that in PHP, feof () is not implemented by directly checking the location of the file pointer relative to the file, but by returning the result based on an identity. The identity is set to "False" after each fseek (), and the identity is set only after a file-content read operation is performed based on the results read by the file.

Based on this guess, you can use two kinds of code logic.

One method is to not do feof () detection, directly detect the content read functions (such as fgetc (), fgets ()) the execution results.

The sample code is as follows:

while ($content = fgets ($fileHandle))!==false) {
   //file content processing ... 
}

This approach, using the PHP is criticized for the return of the function, so you have to use "= = =" or "!==" to detect, can not simplify the code into:

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

Another method is to do a file read first and 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 this example will help you with your PHP programming.

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.