downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

IteratorAggregate> <Iterator::rewind
[edit] Last updated: Fri, 17 May 2013

view this page in

Iterator::valid

(PHP 5 >= 5.0.0)

Iterator::validVérifie si la position courante est valide

Description

abstract public boolean Iterator::valid ( void )

Cette méthode est appelée après Iterator::rewind() et Iterator::next() pour vérifier si la position courante est valide.

Liste de paramètres

Cette fonction ne contient aucun paramètre.

Valeurs de retour

La valeur retournée sera transtypée en booléen, puis, évaluée. Cette fonction retourne TRUE en cas de succès ou FALSE si une erreur survient.



IteratorAggregate> <Iterator::rewind
[edit] Last updated: Fri, 17 May 2013
 
add a note add a note User Contributed Notes Iterator::valid - [3 notes]
up
2
seva dot lapsha at gmail dot com
3 years ago
If Iterator::valid() returns false, the foreach() loop will be terminated.
up
0
Voitcus at gmail dot com
2 months ago
If your class implements also ArrayAccess interface, you could use as valid() body

function valid(){   
   return $this->offsetExists($this->position);
}
up
0
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)
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites