Concatenate strings in foreach loop with array key
<p>I've got a function, which takes in a string parameter for the name of specific page section, this can either be <code>head</code> or <code>body</code>. </p>

<p>The function looks like this:</p>

<pre><code>private $html = [];

public function doStuff($pageSection, array $views)
{
    foreach ($views as $view) {
        //Do some other stuff with view

        $this-&gt;html[$pageSection] .= $view-&gt;renderOutput();
    }

    print_r($this-&gt;html);
}
</code></pre>

<p>The <code>renderOutput()</code> function from <code>$view</code> returns a string, and isn't throwing any errors.</p>

<p>When trying to add this string to the <code>$this-&gt;html</code> array in a specific array key (the <code>$pageSection</code>), I get the following error:</p>

<blockquote>
  <p>Notice: Undefined index: body in
  C:\file.php on line 35</p>
</blockquote>

<p>While in the next line (<code>print_r($this-&gt;html);</code>, the array is filled with all the strings I wanted to add in the right array key, <code>body</code>.</p>

<p>The function get's called like this:</p>

<pre><code>doStuff('body', array(
           //Array of views
         ));
</code></pre>

<p>I tried to remove the concatenate operator and just change the string inside the array key, this threw the same error.</p>

<p>How is this happening? Since I already added the array key in the <code>$this-&gt;html[$pageSection]</code> part.</p>

<p>Also, how can I fix this problem?</p>