简体   繁体   English

为什么我的PHP脚本会以循环结尾?

[英]Why does my PHP-script end in a loop?

I wrote a simple PHP scripts which should print 320 random generated ages. 我写了一个简单的PHP脚本,可以打印320个随机生成的年龄。 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. 我写了下面的代码,除了getDifference函数,其他所有东西都起作用。 If I leave the function out, 320 ages will be generated. 如果我不使用此功能,将生成320个年龄段。

But if I leave the function in it: 但是,如果我将功能保留在其中:

1) Doesn't work right 1)不能正常工作
2) Stays in a long loop 2)保持长时间循环
3) Sometimes prints out 3)有时打印出来
4 ages instead of the 320. 4岁,而不是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. 其次,您永远$newdifference在循环内修改$newdifference Basically, that third loop condition statement ( $newdifference -12 ) doesn't actually do anything. 基本上,第三个循环条件语句( $newdifference -12 )实际上不执行任何操作。 It subtracts 12, but doesn't do anything with the result of that subtraction. 它减去12,但对该减法的结果不做任何事情。 So if the loop begins (because the condition is true ), then it will never end (because the condition will always be true ). 因此,如果循环开始(因为条件为true ),那么它将永远不会结束(因为条件将始终为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: 它陷入循环中,因为您没有在for循环中增加$i

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. 您需要使用-=而不是-因为否则它不会更新$newdifference的值,而只是返回方程式的结果。

You're not changing $newdifference at all. 您根本不需要更改$ newdifference。 Your for loop should be 您的for循环应为

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

This will decrement $newdifference by 12. 这会将$newdifference减12。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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