Monday, December 15, 2025
HomeLanguagesPHP SplFileObject next() Function

PHP SplFileObject next() Function

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.

Syntax

public 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 neveropen

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 neveropen

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32448 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6817 POSTS0 COMMENTS
Nicole Veronica
11954 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12031 POSTS0 COMMENTS
Shaida Kate Naidoo
6955 POSTS0 COMMENTS
Ted Musemwa
7202 POSTS0 COMMENTS
Thapelo Manthata
6899 POSTS0 COMMENTS
Umr Jansen
6884 POSTS0 COMMENTS