How can you modify PHP code to output only a specific number of lines, such as the first 5 lines?

To output only a specific number of lines, such as the first 5 lines, you can modify the PHP code to read the file line by line and keep track of the line count. Once the desired number of lines have been outputted, you can break out of the loop to stop reading the file.

$file = fopen("example.txt", "r");
$lineCount = 0;

while (($line = fgets($file)) !== false) {
    echo $line;
    $lineCount++;

    if ($lineCount == 5) {
        break;
    }
}

fclose($file);