Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My script stops after a foreach loop

Tags:

foreach

php

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

like image 573
RSilva Avatar asked Aug 24 '26 22:08

RSilva


2 Answers

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

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.

like image 39
ConroyP Avatar answered Aug 28 '26 09:08

ConroyP