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.