Gregws
Gregws

Reputation: 35

PHP - returning variable producing weird outcome

I'm having a weird issue where echo'ing variables is working but $str .= is producing strange outcomes.

    function displayComments($comments, $str){
        foreach ($comments as $info) {
            $str .= $info['id'];
            if (!empty($info['childs'])) {
                $this->displayComments($info['childs'],$str);
            }
        }
        return $str;
       }

     $comments = $this->produceComments($id);
            if(!$comments){
                $str ='
                    <tr>
                        <td>There are no comments for this Project</td>
                    </tr>';
            }else{
                $str = $this->displayComments($comments,'');        
            }

echo $str;

This echos 1,2,3.

The Correct output is 1,2,5,6,3,4 which is output when using

foreach ($comments as $info) {
                echo $info['id']; 

Next I tried building with $str .= and echoing the function

function displayComments($comments,$str=FALSE){
    foreach ($comments as $info) {
        $str .= $info['id'];
        if (!empty($info['childs'])) {
            $this->displayComments($info['childs']);
        }
    }
    echo $str;
    return $str;
  }

This produces 5,6,4,1,2,3 which is out of order and weird.. and when also echo'ed outside of the function it produces 1,2,3 Why does echo'ing the $info['id'] output correctly, but building the values into $str not work and also returning $str cuts values out.

Also why does echo'ing inside the function with $str produce a different combination inside the function as opposed to outside after a return.

The array

Array
   (
       [1] => Array
           (
               [0] => 1
               [id] => 1
               [1] => 1
               [project_id] => 1
               [2] => 0
              [parent] => 0
              [3] => First post
              [comment] => First post
              [4] => 
              [user] => 
              [5] => 2014-02-01
              [date] => 2014-02-01
              [childs] => Array
                  (
                  )

          )

      [2] => Array
          (
              [0] => 2
              [id] => 2
              [1] => 1
              [project_id] => 1
              [2] => 0
              [parent] => 0
              [3] => Second Post
              [comment] => Second Post
              [4] => 
              [user] => 
              [5] => 2014-02-01
              [date] => 2014-02-01
              [childs] => Array
                  (
                      [0] => Array
                          (
                              [0] => 5
                              [id] => 5
                              [1] => 1
                              [project_id] => 1
                              [2] => 2
                              [parent] => 2
                              [3] => Reply to 2nd post
                              [comment] => Reply to 2nd post
                              [4] => 
                              [user] => 
                              [5] => 2014-02-05
                              [date] => 2014-02-05
                              [childs] => Array
                                  (
                                  )

                          )

                      [1] => Array
                          (
                              [0] => 6
                              [id] => 6
                              [1] => 1
                              [project_id] => 1
                              [2] => 2
                              [parent] => 2
                              [3] => Reply to 2nd post
                              [comment] => Reply to 2nd post
                              [4] => 
                              [user] => 
                              [5] => 2014-02-05
                              [date] => 2014-02-05
                              [childs] => Array
                                  (
                                  )

                          )

                  )

          )

      [3] => Array
          (
              [0] => 3
              [id] => 3
              [1] => 1
              [project_id] => 1
              [2] => 0
              [parent] => 0
              [3] => Reply to first post
              [comment] => Reply to first post
              [4] => 
              [user] => 
              [5] => 2014-02-19
              [date] => 2014-02-19
              [childs] => Array
                  (
                      [0] => Array
                         (
                             [0] => 4
                             [id] => 4
                             [1] => 1
                             [project_id] => 1
                             [2] => 3
                             [parent] => 3
                             [3] => Reply to first reply
                             [comment] => Reply to first reply
                             [4] => 
                             [user] => 
                             [5] => 2014-02-05
                             [date] => 2014-02-05
                             [childs] => Array
                                 (
                                 )

                         )

                 )

         )

 )

Upvotes: 0

Views: 51

Answers (1)

dave
dave

Reputation: 64695

I'm pretty sure

foreach ($comments as $info) {
        $str .= $info['id'];
        if (!empty($info['childs'])) {
            $this->displayComments($info['childs'],$str);
        }
    }

should be

foreach ($comments as $info) {
        $str .= $info['id'];
        if (!empty($info['childs'])) {
            $str = $this->displayComments($info['childs'],$str);
        }
    }

You are using recursion, but once you actually recurse, you aren't doing anything with the return data, so it's as if it never happened.

Upvotes: 1

Related Questions