PHP method for getting the number of file lines

Source: Internet
Author: User
This article describes how to obtain the number of file lines in PHP. The example shows two methods of php to obtain the number of file lines, for more information about how to obtain the number of file lines in PHP, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

Two implementation methods are provided. although the second method is easy to understand, the first method is the best.

First:

<? Php $ file_path = 'xxx.txt '; // file path $ line = 0; // Number of initialization rows // open the file $ fp = fopen ($ file_path, 'r ') or die ("open file failure! "); If ($ fp) {// get the content of a row of the file. note: php5 is required to support this function. while (stream_get_line ($ fp, 8192, "\ n") {$ line ++;} fclose ($ fp); // close the file} // Number of output rows; echo $ line;?>

Second:

<? Php $ line = count (file ('filename'); echo $ line;?>

The second method is inefficient because you want to save the file content.

I hope this article will help you with 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.