tags:

views:

227

answers:

3

Hi,

My script is acting strange. After a foreach loop, the script stops. I don't have any error, any warning or notice from Apache. This is the code:

foreach($clientFact as $line)
{
    $cliTemp1[] = $line["idcliente"];
    echo "qwerty";
}
echo "123";

If I add an "echo(qwerty")" inside the loop, it will show the "qwerty" but, right after the end of the loop it will not do anything.

Am I missing something?!

Thanks

+4  A: 

The syntax above looks fine, so as a complete shot in the dark here - how big is the $clientFact array? Is it possible that the $cliTemp1 array is getting so large it's tripping a memory limit?

Maybe rather than echoing "qwerty", echo out the contents of $line["idcliente"] on each iteration to be sure you're successfully getting through all elements in $clientFact.

ConroyP
instead of qwerty I just added a counter inside the loop and it stops always in the iteration number 11815.My array has 109247 elements, today. Tomorrow will be more, for sure.
Armadillo
OK, sounds like the OOM problem hit you. Ozzy's suggestion is right
Patrick Cornelissen
+1  A: 

At a guess, you're dying on an error inside the loop because one of your $lines isn't an array like you expect, and the error is being hidden. Try setting error_reporting(E_ALL) before the loop, and possibly ini_set('display_errors', true).

chaos
As I said earlier, Apache do not return any error, warning or notice.
Armadillo
Yeeaaah.... I'm saying that's likely to be because they're getting suppressed. I don't know anything about your php.ini settings, though, so it's just a shot in the dark.
chaos
You have to configure php too to show all errors. By default they are not visible.
Patrick Cornelissen
+7  A: 

Apache wouldnt return an error as its a PHP error. Adding

error_reporting(E_ALL | E_STRICT);

on the top of your page is a very good idea so you can see every error that happens. It could also be your error handler that is not displaying the error and just ending the script.

If it is a problem with your error handler, add

restore_error_handler();

before the error_reporting function

Edit: read your comment about the array index. It definately sounds like a memory limit being reached in PHP if it stops at a specific index every time.

You could use:

ini_set('memory_limit', '100M');

to change your memory limit to 100megs. Not recommended but if it works, its a problem with not enough memory. Try to refactor your program so it uses less memory

Changing memory limit solved my problem. Thanks
Armadillo
Sounds like changing the memory limit *identified* your problem. Since you said in a comment "tomorrow will be more, for sure", I think you have to do something else to *solve* your problem. You should probably figure out a way to do what you need to do without loading the entire $cliTemp1 array all at once.
bmb