0

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
                                 (
                                 )

                         )

                 )

         )

 )

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That's what I thought when I saw this as well. If you are going to build the $str string, define it OUTSIDE of the method being called and only append to it. On the first example there is a '$str =' which is probably causing the weird behavior.
That was exactly the issue. Thanks very much Dave.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.