3

I use this code with php:

while (0 != $date1 || $this->counter < 5 ) { // if ($this->true_cahce_failure ) { $this->url= $this->adjust_url_with_www_add($this->url); // $this->counter=2; // } $this->cache_debug("Date".$date1." ".$this->url,"Recursion ".$this->counter); $date1 = $this->get_date(); $this->counter++; } $this->cache_debug("Date: ".$date1." ".$this->url,"Loop Done "); 

Basically the loop should continue until either $date is not 0 or counter isn't bigger than 5. Sometimes the date1 has got 0 but sometimes not. When it doesn't it should return in the while evaluation, and stop the iteration. But it doesn't do so and the iteration continues.

It only stops with counter reaches 5. Why is that?

2 Answers 2

3

I think you want to use && in your condition instead of ||.

The way you've written it it will keep going if either condition is true, so it will only stop if they are both false.

EDIT: after reading your question more closely, I think you need to use

while (0 == $date1 && $this->counter < 5 ) { 
Sign up to request clarification or add additional context in comments.

1 Comment

it didnt work.. it exits on the first iteration //.. when date is 0
2

It's likely not stopping because your $this->counter is still < 5 when $date1 is not 0. If you look at those statements in their true/false values, your while condition becomes:

while( false || true ) 

while $this->counter is < 5 and for any values of $date, which would make the execution continue.

You likely want to switch it to

while( 0==$date1 && $this->counter<5 ) 

Using && because you want both conditions to be true, not just one. Also switching the != to == when comparing the date to 0 so it will also stop when you have a non-0 date.

1 Comment

Okay, I used this: while( strlen($date1)<2 && $this->counter<5 ){

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.