简体   繁体   中英

Why does my PHP-script end in a loop?

I wrote a simple PHP scripts which should print 320 random generated ages. These ages should be generated in pairs, and the difference between the ages should be calculated. I wrote the following code, everything but the getDifference function works. If I leave the function out, 320 ages will be generated.

But if I leave the function in it:

1) Doesn't work right
2) Stays in a long loop
3) Sometimes prints out
4 ages instead of the 320.

I need some help.

    <?php
//Minimum of the age range.
$min = 8;

//Maxium of the age range.
$max = 25;

//Generating the ages
for ($i=0;$i<160;$i++) {
  $age1= random_float($min,$max);
  $firstage= round($age1,1);
  $age2= random_float($min,$max);
  $secondage=round($age2,1);
  $diff = getDifference ($age1, $age2);
  print "Age 1: $firstage  Age 2: $secondage Difference: $diff \n ";

}

//Function to generate a random float number
function random_float ($min,$max) {
   return ($min+lcg_value()*(abs($max-$min)));
}

// Function to calculate the differences between the ages in years and months
function getDifference ($age1, $age2) {
  $difference = abs($age2 - $age1);
  $jaar = 0;
  $newdifference = round($difference,1) * 12;
  for ($newdifference; $newdifference >=12; $newdifference -12) {
    $jaar ++; }
  $month = $newdifference - 12*$jaar;
  $newmonth = floor($month);
  $test = "$jaar years and $newmonth months";
  return $test;
}
?>

In this loop:

for ($newdifference; $newdifference >=12; $newdifference -12) {
    $jaar ++;
}

Two things:

First, you don't need that first loop condition statement. The variable already exists, there's nothing to declare:

for (; $newdifference >=12; $newdifference -12) {
    $jaar ++;
}

Second, you never modify $newdifference within the loop. Basically, that third loop condition statement ( $newdifference -12 ) doesn't actually do anything. It subtracts 12, but doesn't do anything with the result of that subtraction. So if the loop begins (because the condition is true ), then it will never end (because the condition will always be true ). Perhaps you meant to modify the value in that last loop condition statement?:

for (; $newdifference >=12; $newdifference -= 12) {
    $jaar ++;
}

It's stuck in a loop because you're not incrementing your $i inside your for loop:

Replace:

for ($newdifference; $newdifference >=12; $newdifference -12)

By:

for ($newdifference; $newdifference >=12; $newdifference -= 12)

You need to use the -= instead of the - because otherwise it's not updating the value of $newdifference , it's just returning the result of the equation.

You're not changing $newdifference at all. Your for loop should be

for($newdifference; $newdifference >= 12; $newdifference -= 12) {

This will decrement $newdifference by 12.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM