1

I have simplified this. I have this line of code in a method in a (codeigniter framework) controller:

$the_result = $this->loop1($ordered_bookings);

And this is is the 'loop1' function:

public function loop1($ob, $count=1):array {
    ++$count;
    if($count < 5)
    {
        $this->loop1($ob, $count);
    }
    else
    {
        return array(1, 2, 3);
    }
}

What I need to be able to do is loop through loop1 function until certain conditions are met before then returning the results array. Problem is calling the function again at the end of loop1 seems to be returning to the original call, and returning nothing at that (obviously I guess).

If I change the loop1 function to simply this:

public function loop1($ob):array {
    return array(1, 2, 3);
}

It works perfectly.

I haven't done this sort of PHP work for a long time and the need to declare a returnType itself was new to me. How can I achieve what I'm aiming for here?

8
  • loop or loop1 inside count condition? Commented Oct 5, 2020 at 21:07
  • 1
    @nice_dev GAH! Sorry, typo. loop should have been loop1. It calls itself again. I have edited the question so it is correct. My apologies. Commented Oct 5, 2020 at 21:16
  • 2
    return $this->loop1(... you are missing the return Commented Oct 5, 2020 at 21:21
  • 1
    @nice_dev Oh my WORD that's so simple! Got it working perfectly! WOW! Just goes to show how long it's been! Thank you. Submit it as answer and I'll gladly accept it. Thanks! Commented Oct 5, 2020 at 21:24
  • 2
    @nice_dev I don't mind if you close this question, but my omitting of 'return' wasn't a typo - I didn't think it required it as I was calling the function again rather than returning to where it was originally called. If I had googled and found this question answered here, however simple the answer, the problem would've been solved for me. But close it if you must. Thanks for your help. Commented Oct 5, 2020 at 21:29

1 Answer 1

1

Your function returns the array only when $count it reaches 5. However, when it finally returns the array, you do nothing with it. That is why it gives you nothing. If you want to do something with the array, you need to capture it when you call loop1 inside itself and pass it along with a return like so:

public function loop1($ob, $count=1):array {
    ++$count;
    if($count < 5)
    {
        return $this->loop1($ob, $count);
    }
    else
    {
        return array(1, 2, 3);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I didn't realise you had to use return if calling the function again. I only thought you needed to use return when returning to the original call. Your explanation solved it for me. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.