To expand on user3720435's answer, you are using a lot of memory by creating a new $api
variable each time you run the function. To understand why, let's "unroll" the code - imagine it was all written out in sequence with no function calls:
$api1 = GetConectApi ($index1, offset1)-> Getrecords ();
foreach ($api1 => $value1) {
// Here is my necessary loop
}
if (count ($api1) > 0) {
// RECURSION HAPPENS HERE
$index2 = $index1 + 200, $offset2 = $offset1 + 200
$api2 = GetConectApi ($index, offset)-> Getrecords ();
foreach ($api2 => $value2) {
// Here is my necessary loop
}
if (count ($api2) > 0) {
// RECURSE AGAIN, AND AGAIN, AND AGAIN
}
}
Note that I've renamed all the variables as $api1
, $api2
, etc. That's because each time you run the function, $api
is actually a different variable. It has the same name in your source code, but it doesn't represent the same piece of memory.
Now, PHP doesn't know you're not going to use $api1
again when you create $api2
, so it has to keep both in memory; as you end up with more and more sets of data, it needs more and more memory.
user3720435's suggestion is to add unset($api)
before the recursion:
$api = GetConectApi ($index, offset)-> Getrecords ();
foreach ($api => $value) {
// Here is my necessary loop
}
if (count ($api) > 0) {
unset($api);
// code as before
}
This tells PHP that you don't need that memory any more, so as it recurses, it won't build up. You'll still build up multiple copies of $index
and $offset
, but these are likely to be very small in comparison.
All that being said, it's not clear why you need recursion here at all. The whole thing could actually be changed to a simple loop:
do {
$api = GetConectApi ($index, offset)-> Getrecords ();
foreach ($api => $value1) {
// Here is my necessary loop
}
$index = $index + $offset;
} while (count ($api) > 0)
A do..while loop always executes once, and then keeps repeating until the condition becomes false. Unrolled it looks like this:
// do...
$api = GetConectApi ($index, offset)-> Getrecords ();
foreach ($api => $value1) {
// Here is my necessary loop
}
$index = $index + $offset;
if (count ($api) > 0) { // while...
$api = GetConectApi ($index, offset)-> Getrecords ();
foreach ($api => $value1) {
// Here is my necessary loop
}
$index = $index + $offset;
}
if (count ($api) > 0) { // while...
// etc
Note that we don't need to allocate any extra memory as we go round the loop, because we haven't entered a new function - we're just using the same variable over and over again.