1

Is there a way to save the value of variables while the recursive call is made? What will happen to $totalNumResources and $totalSize?

I want PHP to hold the variables that will be returned once the start function returns from the recursive call. I don't want it to be modified as a parameter.

This is not duplicate with question here.

function start($URL) {

    if (!check_if_html($URL)) {
        $totalSize = get_remote_file_size($URL);
        echo "Download Size: $totalSize Bytes ";
        $totalNumResources += 1; //single resource is an HTTP request
        echo "HTTP requests: $totalNumResources";

        return;
    }

    $totalNumResources += 1;

    foreach ($html->find('iframe') as $element) {
        echo "IFRAME" . "$element->src.\n";
        start($element->src);
    }
}
Community
  • 1
  • 1
Josip Ivic
  • 3,639
  • 9
  • 39
  • 57
  • 1
    possible duplicate of [Understanding how recursive functions work](http://stackoverflow.com/questions/25676961/understanding-how-recursive-functions-work) – Joeblade Sep 23 '15 at 14:09
  • 1
    @Joeblade I studied that topic for last hour, there's no explanation regarding my question on that topic. – Josip Ivic Sep 23 '15 at 14:15

1 Answers1

1

$totalNumResources and $totalSize are local variables in your function, therefore they exist in the function block. When your function is called recursively, the new function call will create variables with the same names and will initialize them. It is not clear what you intend to do with $totalSize accross recursion, but if you want to be aware of the values of $totalNumResources, you can modify it to be a parameter, like this:

function start($URL, $totalNumResources = 0) {

    if (!check_if_html($URL)) {
        $totalSize = get_remote_file_size($URL);
        echo "Download Size: $totalSize Bytes ";
        $totalNumResources += 1; //single resource is an HTTP request
        echo "HTTP requests: $totalNumResources";

        return;
    }

    $totalNumResources += 1;

    foreach ($html->find('iframe') as $element) {
        echo "IFRAME" . "$element->src.\n";
        $totalNumResources += start($element->src, $totalNumResources);
    }

    return $totalNumResources;
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • I want PHP to hold the variables that will be returned once the start function returns from the recursive call. I don't want it to be modified as a parameter. I'll put this in my question too. Just need to know if it's doable, and possible solutions for it. – Josip Ivic Sep 23 '15 at 14:27
  • Those variables will not be returned by themselves. You need to return them yourself. You can use global variables as well, but that's not a good idea. Instead, you could create a class and put these variables there as static members. – Lajos Arpad Sep 23 '15 at 14:32
  • 2
    Got it now. I can return it like an array too, no need for new class. Tnx for clearing me that! – Josip Ivic Sep 23 '15 at 14:39
  • I am glad I could help. – Lajos Arpad Sep 23 '15 at 17:37