(PHP 5 >= 5.0.0)
Iterator::valid — 检查当前位置是否有效
此方法在 Iterator::rewind() 和 Iterator::next() 方法之后被调用以此用来检查当前位置是否有效。
此函数没有参数。
返回将被转换为布尔型。成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Voitcus at gmail dot com (2013-03-23 21:08:49)
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 (2011-09-26 16:12:12)
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)
?>
seva dot lapsha at gmail dot com (2009-08-01 18:19:48)
If Iterator::valid() returns false, the foreach() loop will be terminated.