php 中允许的内存大小为 33554432 字节耗尽(尝试分配 93 字节)错误
我插入了以下代码:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人所说,您创建了一个无限循环。请改用
for
循环。想一想:在 while 循环中,您测试是否
$_POST['additional_contact1'] != ''
。但你永远不会再改变这个值。因此,一旦为!= ''
,while 循环中的条件始终计算为true
。As the others say, you created an infinite loop. Use a
for
loop instead.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 totrue
.也许循环的条件永远不会计算为 false?
Maybe the condition of the loop never evaluates to false?
您可能有一个无限循环,它会消耗内存,直到没有更多内存可用为止。
如果这三个值在主体内没有改变,则您创建了 inifite 循环。您可能想使用
if
代替,但我不知道整个上下文。You might have an infinite loop there which consumes memory until no more memory is available.
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.