Liam Fell
Liam Fell

Reputation: 1320

PHP: Issue with while loop condition

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

Upvotes: 0

Views: 58

Answers (3)

Martin_83
Martin_83

Reputation: 51

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;

Upvotes: 4

Atif Tariq
Atif Tariq

Reputation: 2772

Use it like this:

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

Upvotes: 5

MortenSickel
MortenSickel

Reputation: 2200

The 2nd line should read

$tot_age = $tot_age - 31536000;

Upvotes: 4

Related Questions