PHP: Read Specific Line From File

In PHP, you can read a specific line from a file using the file() function, which reads the entire file into an array, with each line of the file as an element in the array. Once you have the array, you can access a specific line using the array index.

<?php

$lines = file('file.txt');
$line_number = 5;
echo $lines[$line_number - 1];

Note that this method reads the entire file into memory, so it may not be suitable for very large files.

Alternatively, you can use the fopen(), fgets() and fclose() functions to read a specific line from a file.

<?php

$file = fopen("file.txt", "r");
$line_number = 5;
for ($i = 1; $i < $line_number; $i++) {
    fgets($file);
}
$specific_line = fgets($file);
echo $specific_line;
fclose($file);

This method reads the file line by line and only loads the specific line into memory, so it is more efficient for large files.