phpday 2027 - Call for Papers

CachingIterator::hasNext

(PHP 5, PHP 7, PHP 8)

CachingIterator::hasNext — Check whether the inner iterator has a valid next element

Beschreibung

public function CachingIterator::hasNext(): bool

Checks whether there is an element after the current one.

As the CachingIterator is one element ahead of its inner iterator, this checks whether the inner iterator is valid, which makes it possible to tell whether the current element is the last one before moving past it.

Parameter-Liste

Diese Funktion besitzt keine Parameter.

Rückgabewerte

Returns true if there is an element after the current one, false otherwise.

Beispiele

Beispiel #1 Detecting the last element

<?php
$fruits = new CachingIterator(new ArrayIterator(['apple', 'banana', 'cherry']));

foreach ($fruits as $fruit) {
    echo $fruit;

    if ($fruits->hasNext()) {
        echo ', ';
    }
}
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

apple, banana, cherry

Siehe auch

+add a note

User Contributed Notes 1 note

up
7
andresdzphp at php dot net
15 years ago
CachingIterator::hasNext Example

<?php
$ait = new ArrayIterator(array('Value 1', 'Value 2', 'Value 3', 'Value 4'));
$cit = new CachingIterator($ait);

foreach ($cit as $value) {
    echo $value;
    //if has a next value, print a comma
    if ($cit->hasNext()) {
        echo ', ';
    }
}
?>

Result: Value 1, Value 2, Value 3, Value 4
To Top