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

发布于 2024-08-25 06:30:36 字数 1226 浏览 6 评论 0原文

我插入了以下代码:

$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还是不行。我的代码做错了什么导致它需要这么多内存?我该如何解决这个问题?

I inserted the following code:

$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++;
}

and I received this error: Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 93 bytes)

I tried to increase the memory limit with ini_set(), but it still won't work at 96M. What am I doing wrong with my code to make it need so much memory? How can I solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南城追梦 2024-09-01 06:30:36

正如其他人所说,您创建了一个无限循环。请改用 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

As the others say, you created an infinite loop. Use a for loop instead.

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;
  }
}

Think about it: In your while loop, you test whether $_POST['additional_contact1'] != '' or not. But you never change that value again. So once it is != '', the condition in the while loop always evaluates to true.

套路撩心 2024-09-01 06:30:36

也许循环的条件永远不会计算为 false?

Maybe the condition of the loop never evaluates to false?

北城半夏 2024-09-01 06:30:36

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

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

如果这三个值在主体内没有改变,则您创建了 inifite 循环。您可能想使用 if 代替,但我不知道整个上下文。

You might have an infinite loop there which consumes memory until no more memory is available.

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

If those three values do not change within the body, you created and inifite loop. You might want to use if instead, but I dont know the whole context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文