php 35 lines · 7 steps

Streaming file lines with a PHP generator

A generator reads a file line by line so you can scan huge files without loading them into memory.

Explained by highlit
1<?php
2 
3declare(strict_types=1);
4 
5function readLines(string $path): \Generator
6{
7 $handle = fopen($path, 'rb');
8 
9 if ($handle === false) {
10 throw new \RuntimeException("Unable to open file: {$path}");
11 }
12 
13 try {
14 $lineNumber = 0;
15 
16 while (($line = fgets($handle)) !== false) {
17 yield ++$lineNumber => rtrim($line, "\r\n");
18 }
19 } finally {
20 fclose($handle);
21 }
22}
23 
24function countMatchingLines(string $path, string $needle): int
25{
26 $matches = 0;
27 
28 foreach (readLines($path) as $number => $line) {
29 if (str_contains($line, $needle)) {
30 $matches++;
31 }
32 }
33 
34 return $matches;
35}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Generators let you process arbitrarily large files with constant memory by yielding one item at a time.
  2. 2A finally block guarantees resources are released even when the consumer stops iterating early or an error occurs.
  3. 3Yielding keys alongside values lets a generator carry context like line numbers to its consumer for free.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Streaming file lines with a PHP generator — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code