[lang-ref] ( example_file_line_count ) ( php )
<?php
public function testExampleFileLineCountAlternative(): void
{
// sum(1 for _ in f)
$lines = ['test', 'test', 'test'];
$text = implode("\n", $lines);
$p = $this->tmpDir . DIRECTORY_SEPARATOR . 'a.txt';
file_put_contents($p, $text);
$fp = fopen($p, 'r');
$this->assertNotFalse($fp);
$ct = 0;
while (fgets($fp) !== false) {
$ct++;
}
fclose($fp);
$this->assertSame(3, $ct);
}