title: Fatal error when passing parameter by reference
body: <p>In our PHP app there is a function to send a mail with <code>PHPMailer</code> while collecting the logs of the sending process:</p>
  
  <pre><code>function sendWithLog(PHPMailer $mail, &amp;$debug_log, $log_level = PHPMailer_SMTP::DEBUG_CONNECTION) {
      $debug_log = [];
      $mail-&gt;SMTPDebug = $log_level ?: PHPMailer_SMTP::DEBUG_CONNECTION;
      $mail-&gt;Debugoutput = function($message) use(&amp;$debug_log) {
          $debug_log[] = $message;
      };
  
      $sent = $mail-&gt;send();
  
      $debug_log = !$sent
                  ? $mail-&gt;ErrorInfo
                  . ($debug_log ? "\n" . implode("\n", $debug_log) : "")
                  : "";
  
      return $sent;
  }
  </code></pre>
  
  <p>So, the variable <code>$debug_log</code>, passed by reference, should first be initialized as an empty array, and at the end it is made into a string.</p>
  
  <p>If the function is called again, passing as <code>$debug_log</code> the <code>$debug_log</code> of the previous call, I see this error:</p>
  
  <blockquote>
    <p>PHP Fatal error:  Uncaught Error: [] operator not supported for strings</p>
  </blockquote>
  
  <p>However, the variable should still be initialized as empty array before being bound to the closure with the <code>use()</code> statement, so I really do not understand how this error can happen.</p>
  
  <p>I tried reproducing the behaviour with a simplified version of the code <a href="http://sandbox.onlinephpfunctions.com/code/7f34804f5581f7adbf9de605aae8fcb265d2e24f" rel="nofollow noreferrer">here</a>, but it doesn't seem to trigger the error.</p>
  
  <p>EDIT:</p>
  
  <p>I have solved the problem by changing the function like this:</p>
  
  <pre><code>function sendWithLog(PHPMailer $mail, &amp;$debug_log, $log_level = PHPMailer_SMTP::DEBUG_CONNECTION) {
      $messages = []; // &lt;-- different variable for collecting the log messages!
      $mail-&gt;SMTPDebug = $log_level ?: PHPMailer_SMTP::DEBUG_CONNECTION;
      $mail-&gt;Debugoutput = function($message) use(&amp;$messages) {
          $messages[] = $message;
      };
  
      $sent = $mail-&gt;send();
  
      $debug_log = !$sent
                  ? $mail-&gt;ErrorInfo
                  . ($messages ? "\n" . implode("\n", $messages) : "")
                  : "";
  
      return $sent;
  }
  </code></pre>
  
  <p>However, I am still wondering how it could trigger the error in the first place.</p>
  
  <p>An example of usage is the following:</p>
  
  <pre><code>// $users -&gt; query result from database
  while($user = $users-&gt;fetch()) {
    // build mail and then
    $sent = sendWithLog($mail, $mail_log);
    // here $mail_log should be initialized to NULL the first time by the PHP engine
    // and in subsequent calls it is the log string of the previous call which should be
    // reset to [] first thing inside the function call
  
    if(!$sent) {
      error_log("ERROR SENDING MAIL: $mail_log");
    }
  }
  </code></pre>
  
  <p>EDIT 2</p>
  
  <p>Here is a sample stack trace:</p>
  
  <pre><code>PHP Fatal error:  Uncaught Error: [] operator not supported for strings in /opt/agews64/www/includes/class/AGEws/Mail.php:134
  Stack trace:
  0 /opt/agews64/www/includes/class/PHPMailer/SMTP.php(220): AGEws_Mail::{closure}('CLIENT -&gt; SERVE...', 1)
  1 /opt/agews64/www/includes/class/PHPMailer/SMTP.php(991): PHPMailer_SMTP-&gt;edebug('CLIENT -&gt; SERVE...', 1)
  2 /opt/agews64/www/includes/class/PHPMailer/SMTP.php(885): PHPMailer_SMTP-&gt;client_send('QUIT\r\n')
  3 /opt/agews64/www/includes/class/PHPMailer/SMTP.php(827): PHPMailer_SMTP-&gt;sendCommand('QUIT', 'QUIT', 221)
  4 /opt/agews64/www/includes/class/PHPMailer.php(1721): PHPMailer_SMTP-&gt;quit()
  5 /opt/agews64/www/includes/class/PHPMailer.php(670): PHPMailer-&gt;smtpClose()
  6 /opt/agews64/www/includes/class/AGEws/Mail.php(147): PHPMailer-&gt;__destruct()
  7 /opt/agews64/www/includes/class/AGEws/Mail.php(118): AGEws_Mail::sendWithLog(NULL, 'SMTP Error: Dat...', 3)
  8 /opt/agews64/www/includes/class/AGEws/Cron/Import.php(154): AGEws_Mail::prepareAndSendWithLog('SMTP Error: Dat...', Array, 'Risu in /opt/agews64/www/includes/class/AGEws/Mail.php on line 134
  </code></pre>