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 8105313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:07:46+00:00 2026-06-06T00:07:46+00:00

sorry about the funky title, I was having trouble coming up with one. First

  • 0

sorry about the funky title, I was having trouble coming up with one. First off, I managed to actually solve my problem (after tearing apart my code), but I have no idea WHY my solution worked and I am trying to treat this as a learning experience.

I have a class which collects info from the db. This class tracks an index value so that the calling function can grab the results one at a time. So my class code originally contained…

protected $index = NULL;
protected $array = array(stuff);

public function next_item()
{
    if($this->index == NULL)
    {//first time called
        $this->index = 0;
    }
    else
    {
        $this->index++;
    }

    if($this->index < sizeof($this->array))
    {
          return TRUE;
     }
      return FALSE;
 }

 public function get_result()
 {
      return $this->array[$this->index];
  }

And the calling function has something akin to

 while($myclass->next_item()){
        echo $myclass->get_result();
 }

It’s all a lot more involved than this of course, but this is the problem code (and you get the idea). This code induced an infinite loop (or the appearance of one) in my project. I fixed my problem by initializing $index to -1, and removing the conditional check for null. So it’s now …

protected $index = -1;

public function next_item()
{
    $this->index++;

    if($this->index < sizeof($this->array))
    {
          return TRUE;
    }
    return FALSE;
}

My question is, WHY did that work? The index was never referenced in the array while it was NULL, I checked. It was always after the conditional check for NULL was called and the assignment to 0 occurred.

I really appreciate your time, in advance. Just trying to turn this into a learning experience, and it’s better if I understand why it worked, not just that it did.

I’m sorry if this question is answered somewhere else, I couldn’t figure out how to label my question (as you can see) much less have success finding a previous answer to it.

cheers,
-David

  • 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-06-06T00:07:48+00:00Added an answer on June 6, 2026 at 12:07 am

    Your problem was caused by the fact that 0 == NULL in PHP. So when you set the index to 0 once you would keep setting it to 0 again over and over every time you call next_item, this->index == NULL would always be true.

    Changing your test to this->index === NULL would have also solved your problem. See this question which covers the difference between == and === in PHP.

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

Sidebar

Related Questions

Sorry about the strange title, but I am having a small issue if the
Sorry about the bad title, I couldn't think of a more descriptive one. The
Sorry about the wierd title, feel free to come up with a better one
Sorry about the confusing title, I had a hard time describing the problem. I
I'm sorry about the title I'm just not sure how to describe this one.
First of all I'm sorry about the title, I didn't know how to formulate
Sorry about the stupid question however I can't see/ not good enough to solve
Sorry about a vague title, but here's what i am trying to do. I
Sorry about the title, I couldn't think of a better way to describe the
Sorry about the extremely vague question title (any suggestions for improvements welcome) I have

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.