$file = fopen('example.txt', 'r'); echo fgets($file); echo 'Line number: ' . $file->getLine(); fclose($file);
$file = new SplFileObject('example.txt'); echo $file->fgets(); echo 'Line number: ' . $file->getLine(); $file = null;In this example, we create a SplFileObject instance for the file 'example.txt'. We read the first line using the fgets method and call the getLine method to get the current line number. Finally, we nullify the file object. Package library: Standard PHP Library (SPL)