Allowed memory size of 33554432 bytes exhausted (tried to allocate 93 bytes) error in php
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]['addit开发者_如何转开发ional_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?
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
.
Maybe the condition of the loop never evaluates to false?
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.
精彩评论