How can fopen() and fgets() be used to achieve the desired outcome in this scenario?

To achieve the desired outcome in this scenario, we can use fopen() to open a file in read mode and then use fgets() to read each line of the file until we reach the desired line. By using fgets() in a loop, we can skip over unwanted lines until we reach the line we are interested in.

$file = fopen("data.txt", "r");
if ($file) {
    $desired_line_number = 5;
    $current_line_number = 1;
    
    while (($line = fgets($file)) !== false) {
        if ($current_line_number == $desired_line_number) {
            echo $line;
            break;
        }
        $current_line_number++;
    }
    
    fclose($file);
}