1

Hi I have this code here:

public function length($args) {
    if (isset($this->length)) {
        foreach ($this->length as $k => $v) {
            if (strlen($args[$k])>=$v[0] && strlen($args[$k])<$v[1]) {
                return true;
            } else {
                array_push($this->form_errors, $v[2]);
                return false;
            }
        }
    } else { 
        return true;
    }
}

I'm not sure why but its not working as expected. The foreach loop only loops through one of the $args[$k] even though there are 2 of them. Anyone have any idea whats going on? I'm writing this question in a hurry so if I need to explain anything else, please let me know.

Thanks!

2
  • can you show us print_r($this->length) Commented Aug 26, 2012 at 2:38
  • 1
    put return value into variable and return it at the end of function. Commented Aug 26, 2012 at 2:43

1 Answer 1

9

Your two return statements in both sides of the if() clause terminate the function on the first iteration of the loop. Either the strlen stuff is true and you return true, or they're false and you return false. regardless, the loop ends.

If you simply want to jump to the next iteration, then use continue instead, which'd allow the foreach to proceed.

Sign up to request clarification or add additional context in comments.

1 Comment

But there's no need for a continue, because there's nothing else in the loop.

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.