PHP SplFileObject next() Function Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The SplFileObject::next() is an inbuilt function in PHP that is used to iterate the file using the SplFileObject. The pointer will point next line. The SplFileObject implements Iterator and Traversal. That means you can use it in foreach loops and use many of the iterator functions with it. Syntaxpublic void SplFileObject::next ( void )Parameter This function does not accept any parameters. Return Value This function does not return any value. Program 1: The following program demonstrates the SplFileObject::next() function. Save this text in the "output.txt" file in the current working directory before running this program. Hey GeeksforGeeks PHP <?php $file = new SplFileObject("./output.txt", "r"); while (!$file->eof()) { // Get the current line // without advancing the pointer $line = $file->current(); echo $line . PHP_EOL; // Advance to the next line $file->next(); } ?> Output: Hey GeeksforGeeks Program 2: The following program demonstrates the SplFileObject::next() function. Save this text in the "output.txt" file in the current working directory before running this program. Hello This is a Simple example Another example here. PHP <?php $file = new SplFileObject("./output.txt", "r"); while (!$file->eof()) { // Get the current line $line = $file->current(); if (strpos($line, "example") !== false) { echo $line . PHP_EOL; } // Advance to the next line $file->next(); } ?> Output: Simple example Another example here. Reference: https://www.php.net/manual/en/splfileobject.next.php Comment More infoAdvertise with us Next Article PHP SplFileObject next() Function neeraj3304 Follow Improve Article Tags : PHP PHP-file-handling PHP-function Similar Reads PHP SplFileObject key() Function The SplFileObject::key() is an inbuilt function of the Standard PHP Library (SPL) in PHP that is used to get the key (line number) of the current line pointed to by the SplFileObject. Syntaxpublic SplFileObject::key(): intParameter This function does not have any parameters. Return Value The SplFile 2 min read PHP | SplFileObject fwrite() Function The SplFileObject::fwrite() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to write to the file. Syntax: int SplFileObject::fwrite( $str, $length ) Parameters: This function accept two parameters as mention above and describe below: $str: It is used to specify the 2 min read PHP | SplFileObject eof() Function The SplFileObject::eof() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used reached end of file. Syntax: string SplFileObject::eof( void ) Parameters: This function does not accept any parameter. Return values: Returns TRUE on Success. Below Programs illustrate the Sp 1 min read PHP | SplFileObject seek() Function The SplFileObject::seek() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to seek to specify the line. Syntax: void SplFileObject::seek( $line_num) Parameters: This functions accept only one parameter $line_num which specifies the line number of the file. Return va 1 min read PHP SplObjectStorage next() Function The SplObjectStorage::next() function is an inbuilt function in PHP which is used to move to next entry of storage. Syntax: void SplObjectStorage::next() Parameters: This function does not accept any parameter. Return Value: This function does not return any value. Below programs illustrate the SplO 1 min read PHP | SplFileObject fgets() Function The SplFileObject::fgets() function is an inbuilt function of the Standard PHP Library (SPL) in PHP which is used to get a line from the file. Syntax: string SplFileObject::fgets( void ) Parameters: This function does not accept any parameter. Return values: This function returns a string contain 1 min read PHP SplFileObject fseek() Function The SplFileObject::fseek() function is an inbuilt function of Standard PHP Library (SPL) in PHP that allows you to move the file pointer to a specified position within a file opened using SplFileObject. The file pointer is the position where the next read or write operation will occur. This function 2 min read PHP | SplFileObject ftell() Function The SplFileObject::ftell() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to return the position of the file pointer which specifies the current offset in the file. Syntax: int SplFileObject::ftell( void ) Parameters: This function does not accept any parameters. 1 min read PHP | SplFileObject fstat() Function The SplFileObject::fstat() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to give the information of the file. Syntax: array SplFileObject::fstat( void ) Parameters: This function does not accept any parameter. Return values: This function returns an array which c 1 min read PHP | SplFileObject fgetc() Function The SplFileObject::fgetc() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to get character from file. Syntax: string SplFileObject::fgetc( void ) Parameters: This function does not accept any parameter. Return values: Returns single character read from the file or 1 min read Like