0

I have the following code, unfortunately it's looping infinity but I can't understand why :

$tot_age is set to 604245119

while ($tot_age > 536467742) {
        $tot_age - 31536000;
        if  ($tot_age < 536467742 ) {
            // do something
            break;
        }
    }

So what I'm attempting to here is the following. If $tot_age is greater than 17 years, iterate through the loop and minus 12 months from $tot_age. I'm then attempting to break out of the loop at the point which $tot_age is less than 17 years. I'll apply some logic here too.

Can anyone see an issue here? Thanks

4
  • 3
    You are not setting $tot_age - 31536000 here,you are just doing an operation in the air as it were.Change it to $tot_age=$tot_age - 31536000; Commented Jan 26, 2016 at 13:58
  • Ah, thank you. It's been so long since I've done even basic arithmetic in PHP I appear to have forgotten how. Commented Jan 26, 2016 at 14:00
  • @Mihai $tot_age -= 31536000; :P Commented Jan 26, 2016 at 14:01
  • Sure that works too. Commented Jan 26, 2016 at 14:01

3 Answers 3

5

Use it like this:

while ($tot_age > 536467742) {
        $tot_age = $tot_age - 31536000;
        if  ($tot_age < 536467742 ) {
            // do something
            break;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

4

The 2nd line should read

$tot_age = $tot_age - 31536000;

Comments

4

Youre not changing the value of tot_age in the loop, you're just making an empty statement. Change:

$tot_age - 31536000;

to:

$tot_age = $tot_age - 31536000;

Comments

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.