views:

330

answers:

3

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?

+3  A: 

Maybe the condition of the loop never evaluates to false?

mizipzor
It is not *maybe* ;)
Felix Kling
I think you might want to calibrate your sarcasm detector, good sir. ;)
mizipzor
@mizipzor: No I know what you meant, but *maybe* the OP didn't ;)
Felix Kling
+1  A: 

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.

Malax
I think you may be right. All I am trying to do is if any of those 3 fields are not empty, then save the values from each of those three groups of fields. First I check if there's something in one of the fields in the group. That's why i start the counter with 1 and run through all the fields I have ending with 1, then I increment the counter and do the same thing again. If there's nothing in the first field in the group, I unset all the variables in that group to kill those sessions. That's why I am using a loop and cannot use an if statement. Unless you see a better solution?
zeckdude
+3  A: 

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.

Felix Kling
This is exactly what I needed. I don't know why I haven't thought of that. Maybe too many hours in front of the computer. Thanks!
zeckdude
@Chris: Sometimes making a break really helps! ;)
Felix Kling