3

我插入了以下代码:

$counter = 1;
while($_POST['additional_contact1'] != '' || $_POST['additional_contact2'] != '' || $_POST['additional_contact3'] != '') {
  if($_POST['additional_contact' . $counter] != '') {
    $_SESSION['contact'][$counter]['additional_contact'] = $_POST['additional_contact' . $counter];
    $_SESSION['contact'][$counter]['additional_int_prefix'] = $_POST['additional_int_prefix' . $counter];
    $_SESSION['contact'][$counter]['additional_prefix'] = $_POST['additional_prefix' . $counter];
    $_SESSION['contact'][$counter]['additional_first'] = $_POST['additional_first' . $counter];
    $_SESSION['contact'][$counter]['additional_last'] = $_POST['additional_last' . $counter];
  } else {
    $_SESSION['contact'][$counter]['additional_contact'] = null;
    $_SESSION['contact'][$counter]['additional_int_prefix'] = null;
    $_SESSION['contact'][$counter]['additional_prefix'] = null;
    $_SESSION['contact'][$counter]['additional_first'] = null;
    $_SESSION['contact'][$counter]['additional_last'] = null;
  }

$counter++;
}

我收到了这个错误:致命错误:允许的内存大小为 33554432 字节已用尽(试图分配 93 字节)

我尝试使用 ini_set() 增加内存限制,但它仍然无法在 96M 时工作。我的代码做错了什么使它需要这么多内存?我怎么解决这个问题?

4

3 回答 3

4

正如其他人所说,您创建了一个无限循环。请改用for循环

for($counter = 1; $counter <= 3; $counter++) {
  if($_POST['additional_contact' . $counter] != '') {
    $_SESSION['contact'][$counter]['additional_contact'] = $_POST['additional_contact' . $counter];
    $_SESSION['contact'][$counter]['additional_int_prefix'] = $_POST['additional_int_prefix' . $counter];
    $_SESSION['contact'][$counter]['additional_prefix'] = $_POST['additional_prefix' . $counter];
    $_SESSION['contact'][$counter]['additional_first'] = $_POST['additional_first' . $counter];
    $_SESSION['contact'][$counter]['additional_last'] = $_POST['additional_last' . $counter];
  } else {
    $_SESSION['contact'][$counter]['additional_contact'] = null;
    $_SESSION['contact'][$counter]['additional_int_prefix'] = null;
    $_SESSION['contact'][$counter]['additional_prefix'] = null;
    $_SESSION['contact'][$counter]['additional_first'] = null;
    $_SESSION['contact'][$counter]['additional_last'] = null;
  }
}

想一想:在你的 while 循环中,你测试是否$_POST['additional_contact1'] != ''。但是你再也不会改变那个值了。所以一旦它是!= '',while 循环中的条件总是计算为true

于 2010-03-17T09:58:31.527 回答
3

也许循环的条件永远不会评估为假?

于 2010-03-17T09:47:23.290 回答
1

您可能在那里有一个无限循环,它会消耗内存,直到没有更多可用内存为止。

while($_POST['additional_contact1'] != '' || $_POST['additional_contact2'] != '' || $_POST['additional_contact3'] != '') {

如果这三个值在正文中没有变化,则您创建了 inifite 循环。您可能想if改用,但我不知道整个上下文。

于 2010-03-17T09:49:29.627 回答