php中允许的内存大小为33554432字节耗尽(试图分配93字节)错误

时间:2010-03-17 09:45:02

标签: php memory fatal-error

我插入了以下代码:

$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下运行。我的代码错误是什么让它需要这么多内存?我该如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

可能循环的条件永远不会评估为假?

答案 1 :(得分:3)

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

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

答案 2 :(得分:1)

你可能有一个无限循环,它消耗内存,直到没有更多的内存可用。

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

如果这三个值在体内没有变化,那么你就创造了并且引入了循环。您可能希望使用if,但我不了解整个上下文。