Jump to content

Unable to print out array from a PHP function


Robinson

Recommended Posts

Dear all, 

Kindly help with this code I have written, it is not returning the array I created. It is a very small script but I have been looking for a very long time but I couldn't see why it went wrong.

$duration = 2;
$start='01-01-2019';
$month =date('n', strtotime($start));
$year =date('Y', strtotime($start));
$mon = 12-$month+1;
$remainingMonth = $duration*12;

$years_arr = array("$year");
$residue_arr = array("$remainingMonth");
$years_arr = process($remainingMonth,$year,$mon,$years_arr,$residue_arr);

print_r($years_arr); //<output

function process($remainingMonth,$year,$mon,$years_arr,$residue_arr){
    $residue = $remainingMonth - $mon;//$D$18-D20
    if($residue != 0){
        $new_year = $year+1;
        array_push($years_arr, $new_year);
        array_push($residue_arr, $residue);
        
        if($residue > 12 ){
            $mon = 12;
        } else {
            $mon = $residue;
        }
        echo "<br>";
        process($residue,$new_year,$mon,$years_arr,$residue_arr);
    } else {
        print_r($years_arr);
        return $years_arr;
    }
}//end of process(..)

The objective is to add a year to the $years_arr array and then print it put, yet I notice that nothing get's printed out. Please assist, if you need more info from my side, please do ask. Thank you very much in advance.

Link to comment
Share on other sites

replace print_r with var_dump to see what the variables actually contain. Check every condition if it really matches your expected values, like var_dump($residue) and so on. As this is a recursive function it may be of interest to have a static iteration number and all given function arguments to display. As this is a working example (good!) you can test this first online to see if may be a configuration issue on your machine

https://3v4l.org/VC646

Edited by chhorn
Link to comment
Share on other sites

The design of your recursive function isn't quite right.

$duration = 2;
$start='01-01-2019';
$month =date('n', strtotime($start));
$year =date('Y', strtotime($start));
$mon = 12-$month+1;
$remainingMonth = $duration*12;

$years_arr = array("$year");
$residue_arr = array("$remainingMonth");
$years_arr = process($remainingMonth,$year,$mon,$years_arr,$residue_arr);           // THIS CALL RETURNS NOTHING

print_r($years_arr); //<output

function process($remainingMonth,$year,$mon,$years_arr,$residue_arr){
    $residue = $remainingMonth - $mon;//$D$18-D20
    if($residue != 0){
        $new_year = $year+1;
        array_push($years_arr, $new_year);
        array_push($residue_arr, $residue);
        
        if($residue > 12 ){
            $mon = 12;
        } else {
            $mon = $residue;
        }
        echo "<br>";
        process($residue,$new_year,$mon,$years_arr,$residue_arr);                    // RETURNS VALUE BUT IS IGNORED
    } else {
        print_r($years_arr);
        return $years_arr;
    }
}//end of process(..)

The first call (lline 11) returns nothing as the if/else calls the process() function instead. This second call does go through the else{} branch and return the array. However your call does not collect the value returned and so, when the function exits, nothing is returned.

You need to capture the result from that internal call to process() and return that value as the result so it is passed up the chain of calls, thus...

function process($remainingMonth,$year,$mon,$years_arr,$residue_arr){
    $residue = $remainingMonth - $mon;//$D$18-D20
    if($residue != 0){
        $new_year = $year+1;
        array_push($years_arr, $new_year);
        array_push($residue_arr, $residue);
        
        if($residue > 12 ){
            $mon = 12;
        } else {
            $mon = $residue;
        }
        echo "<br>";
        $years_arr = process($residue,$new_year,$mon,$years_arr,$residue_arr);         // GET RETURNED VALUE
    } else {
    //  print_r($years_arr);
        return $years_arr;
    }
    return $years_arr;                                                                 // PASS IT BACK
}//end of process(..)

 

[edit] P.S. Is this a competition entry for the most convoluted code to return an array of 2 consecutive year numbers?

Edited by Barand
Link to comment
Share on other sites

Thank you Barand, 

I have tried out your suggestion and it has worked. However, I have one question, it is about these lines:

 } else {
     print_r($years_arr);
        return $years_arr;

How is it that it manages to print $years_arr but it doesn't return the $years_arr. Also, can you point me to some resources(books, websites) that teach about Recursion in PHP, they are hard to come by and most books, at least the ones I obtained, delve on the subject very briefly. And, no, this is not an entry into a competition :)), it is a small portion of a bigger program.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.