Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6126949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:26:33+00:00 2026-05-23T16:26:33+00:00

How can I write a condition that not to run the foreach below if

  • 0

How can I write a condition that not to run the foreach below if the object LimitIterator is empty?

$numbers = array(5, 19, 8, 35, 50);

$iterator = new ArrayIterator($numbers);

$limiter = new LimitIterator($iterator, 5, 2);

foreach($limiter as $number)
{
    echo $number.'<br/>';
}

The code above returns this error,

Fatal error: Uncaught exception 'OutOfBoundsException' with message 'Seek position 5 is out of range' in ..
OutOfBoundsException: Seek position 5 is out of range in..

I just don’t want to run the foreach if the object LimitIterator is empty.

EDIT:

Why does $limiter->valid(); always return false? I have the code below running on a page on my site,

$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/>';
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:26:33+00:00Added an answer on May 23, 2026 at 4:26 pm

    The OutOfBoundsException is thrown when the LimitIterator tries to seek to the starting offset after rewinding, at the very beginning of the foreach loop.

    If you want to test to see if the seek position is okay, then either rewind() or manually seek() within a try/catch block.

    try {
        $limiter->rewind();
    } catch (OutOfBoundsException $e) {
        // Do whatever
    }
    

    Of course, you could instead wrap your foreach loop in a try/catch block.


    Why does $limiter->valid(); always return false?

    It does not always return false, only when it is not at a valid positon.

    The LimitIterator in your script, at the point of calling valid(), has not been told to move anywhere along itself nor the inner iterator. Until rewind() or seek() has been called, there is no way for it to be at a valid position.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how can i write a conditional comment for scripts that should run for all
Suppose I let the user to write a condition using Javascript, the user can
Can someone please explain how you can write a url pattern and view that
In SQL one can write a query that searches for a name of a
How can I write a JQuery code that test if a given variable between
Are there any software platforms out there that can be used to run scripts
I can write: update my_table set xml = updateXML(xml, '/a/b', '1') where document_id =
I can write an assertion message one of two ways. Stating success: assertEquals( objects
We can write CSS as the following types: Inline CSS Embedded CSS External CSS
In C++ one can write any of the following statements: 10; true; someConstant; //if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.