If Iterator::valid() returns false, the foreach() loop will be terminated.
Iterator::valid
(PHP 5 >= 5.0.0)
Iterator::valid — Prüft, ob die aktuelle Position zulässig ist
Beschreibung
abstract public boolean Iterator::valid
( void
)
Diese Methode wird nach Iterator::rewind() und Iterator::next() aufgerufen, um zu prüfen, ob die aktuelle Position zulässig ist.
Parameter-Liste
Diese Funktion hat keine Parameter.
Rückgabewerte
Der Rückgabewert wird in den Typ boolean gecastet und dann
evaluiert.
Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.
seva dot lapsha at gmail dot com ¶
3 years ago
Voitcus at gmail dot com ¶
1 month ago
If your class implements also ArrayAccess interface, you could use as valid() body
function valid(){
return $this->offsetExists($this->position);
}
hyponiq at gmail dot com ¶
1 year ago
Take consideration when utilizing this method and using the returned value of isset([value]). The value of isset([value]) will be false for null values.
I ran into this problem when checking isset([value]) against array elements whose value was null.
Example:
<?php
$array = array(null, 1, 2, 3);
$isSet = isset($array[0]);
var_dump($isSet); // output: boolean(false)
class AClass {
public $array = (null, 1, 2, 3);
function valid() {
return isset($this->array[0]);
}
}
$class = new AClass;
var_dump($class->valid()); // output: boolean(false)
?>
