How to get the number of lines in a file using PHP? Last Updated : 22 Jan, 2021 Comments Improve Suggest changes Like Article Like Report Given a file reference, find the number of lines in this file using PHP. There are a total of 3 approaches to solve this. test.txt: This file is used for testing all the following PHP codes Geeks For Geeks Approach 1: Load the whole file into memory and then use the count() function to return the number of lines in this file. Example: PHP <?php $filePath = "test.txt"; $lines = count(file($filePath)); echo $lines; ?> Output: 3 This approach loads the whole file into memory which can cause a memory overflow issue. Approach 2: Here we will load only one line of files at a time which is better than the1st approach. PHP fopen() function is used to open a file or URL. The PHP fgets() function is used to return a line from an open file. The fclose() function is used to close the file pointer. Example: PHP <?php $filePath="test.txt"; $linecount = 0; $handleFile = fopen($file, "r"); while(!feof($handleFile)){ $line = fgets($handleFile); $linecount++; } fclose($handleFile); echo $linecount; ?> Output: 3 What if 1GB file has only one line in it. This code will load the whole 1GB of data which can cause a memory overflow issue. Approach 3: We will load only a specific size of data into memory. We don't care about line breaks at the time of loading. We will iterate on the loaded data to count the number of line breaks in it. Example: PHP <?php $filePath="test.txt"; $linecount = 0; $handleFile = fopen($filePath, "r"); while(!feof($handleFile)){ // We are loading only 4096 bytes of data at a time. $line = fgets($handleFile, 4096); // We are using PHP_EOL (PHP End of Line) //to count number of line breaks in currently loaded data. $linecount = $linecount + substr_count($line, PHP_EOL); } fclose($handleFile); echo $linecount; ?> Output: 3 This is a better approach in terms of space but, note that we are iterating on our data twice So, the time taken will be double. Comment More infoAdvertise with us Next Article How to get the number of lines in a file using PHP? P pratikraut0000 Follow Improve Article Tags : Technical Scripter Web Technologies PHP Technical Scripter 2020 Similar Reads How to get total number of elements used in array in PHP ? In this article, we will discuss how to get total number of elements in PHP from an array. We can get total number of elements in an array by using count() and sizeof() functions. Using count() Function: The count() function is used to get the total number of elements in an array. Syntax: count(arra 2 min read How to count the number of lines in a CSV file in Python? Counting the number of lines in a CSV file in Python means determining how many rows the file contains, including or excluding the header depending on your needs. For example, if your CSV file has 100 data rows plus one header row, the total line count is 101. We will use the following dataset to de 2 min read How to Count number of Lines in a Git Repository? Counting the number of lines in a Git repository can be useful for various reasons, such as understanding the size of the project or monitoring codebase growth over time. There are several methods to achieve this, including using Git command line tools, the CLOC (Count Lines of Code) tool, and custo 4 min read How to read a Large File Line by Line in PHP ? We will use some file operations to read a large file line by line and display it. Read a file: We will read the file by using fopen() function. This function is used to read and open a file. Syntax: fopen("filename", access_mode); Parameter: filename: Filename is the name of the file access_mode: I 2 min read How to write Into a File in PHP ? In this article, we are going to discuss how to write into a text file using the PHP built-in fwrite() function. The fwrite() function is used to write into the given file. It stops at the end of the file or when it reaches the specified length passed as a parameter, whichever comes first. The file 2 min read Like