Lesson 2
Reading Text Files Line-by-Line in PHP
Introduction and Context Setting

Welcome to another lesson, where we'll explore a fundamental technique in text data manipulation: reading files line-by-line using PHP. In many real-world applications, processing data one line at a time is crucial for effective data management, particularly when dealing with large files like logs or data streams. By the end of this lesson, you'll understand how to efficiently read and process file data line-by-line using PHP.

Understanding the File Function

File handling in PHP can be done using the file() function, which reads the entire file into an array, with each element representing a line:

php
1$lines = file($filePath, FILE_IGNORE_NEW_LINES);

The file() function is called with parameters that specify how the file should be read and how each line should be treated:

  • $filePath: The path to the file from which to read lines.
  • FILE_IGNORE_NEW_LINES: This optional parameter prevents PHP from adding a trailing newline character to each array element, which is useful for precise string manipulation.

To iterate over the lines read from the file, use a foreach loop as shown below:

php
1foreach ($lines as $line) { 2 // Process each line 3}

In this block, $lines contains each line of the file as an array element, and the foreach loop iterates over the array, allowing you to process or manipulate each line individually.

Reading Files Line-by-Line

To read a file line-by-line in PHP, use the file() function and iterate over the results with a foreach loop. Let's see this in action:

php
1$filePath = 'data.txt'; 2echo "Reading file line-by-line:\n"; 3 4$lines = file($filePath, FILE_IGNORE_NEW_LINES); 5 6foreach ($lines as $line) { 7 echo $line . "\n"; // Output each line to the console 8}

Here, file($filePath, FILE_IGNORE_NEW_LINES) reads lines from data.txt one-by-one. Each line is stored in the $line variable and then printed to the console.

Reading Integers from a File and Finding Their Sum

Imagine needing to process a set of numbers stored in a file — reading each integer and calculating their sum. This is a practical task you can accomplish using the file() function with ease.

Assume numbers.txt contains:

Plain text
110 220 330 440

The following PHP code reads integers from this file and calculates their sum:

php
1<?php 2 3$filePath = 'numbers.txt'; 4$totalSum = 0; 5 6$lines = file($filePath, FILE_IGNORE_NEW_LINES); 7 8foreach ($lines as $line) { 9 if (is_numeric($line)) { 10 $totalSum += (int) $line; // Add the integer to totalSum 11 } 12} 13 14echo "The sum of the numbers is: " . $totalSum . "\n"; 15 16?>
  • Reading Numbers: Each line is checked to be numeric and parsed to an integer.
  • Calculating Sum: The integers are added together in the $totalSum variable.

After executing this code, the output displays the total sum of the numbers:

Plain text
1The sum of the numbers is: 100
Summary and Practice Preparation

In this lesson, you gained the skills to read a text file line-by-line using PHP — a fundamental technique for processing large datasets efficiently. You've learned to manage file I/O operations using the file() function and how to handle each line effectively.

Now, you're ready to dive into practice exercises where you can apply these concepts and strengthen your understanding. Continue to build on these skills as we explore further parsing techniques in future lessons. Keep up the great work!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.