The feof function in PHP may cause an endless loop.

Source: Internet
Author: User
Some time ago, a company often reported timeout on a page, which was originally thought to be caused by sending an email. After the mail is split to asynchronous sending, the timeout problem is not solved. So I carefully studied the code segments near timeout. I found a similar code :? Php?filefopencontacts.csv, r; while! Feof $ fileprint_rfgetc

Some time ago, a company often reported timeout on a page, which was originally thought to be caused by sending an email. After the mail is split to asynchronous sending, the timeout problem is not solved. So I carefully studied the code segments near timeout. I found a similar code :? Php $ file = fopen "contacts.csv", "r"; while! Feof $ file print_rfgetc

Some time ago, a company often reported timeout on a page, which was originally thought to be caused by sending an email. After the mail is split to asynchronous sending, the timeout problem is not solved. So I carefully studied the code segments near timeout.

The following code is found:

 

In fact, the above Code is the sample code of W3scholl.

If I have not carefully read the PHP manual, I cannot see any problems with this code?

The key to the problem lies in the feof function, which is clearly described in the Manual:

If the passed file pointer is not valid you may get an infinite loop, because feof() fails to return TRUE.

See: http://cn2.php.net/feof

Therefore, if the above Code fails to open the file, it will lead to an endless loop, resulting in page timeout.

Comments | Author: tomheng 85159e08d5f1f50d244825cb03e92c65

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.