foreach
如果对象 LimitIterator 为空,如何编写不运行以下条件的条件?
$numbers = array(5, 19, 8, 35, 50);
$iterator = new ArrayIterator($numbers);
$limiter = new LimitIterator($iterator, 5, 2);
foreach($limiter as $number)
{
echo $number.'<br/>';
}
上面的代码返回这个错误,
Fatal error: Uncaught exception 'OutOfBoundsException' with message 'Seek position 5 is out of range' in ..
OutOfBoundsException: Seek position 5 is out of range in..
foreach
如果对象 LimitIterator 为空,我只是不想运行。
编辑:
为什么$limiter->valid();
总是返回false
?我有下面的代码在我网站的一个页面上运行,
$numbers = array(5, 19, 8, 35, 50);
$iterator = new ArrayIterator($numbers);
$limiter = new LimitIterator($iterator, 0, 2);
var_dump($limiter->valid());
if ($limiter->valid())
{
foreach($limiter as $number)
{
echo $number.'<br/>';
}
}