title: Passing database connection by reference in PHP
body: <p>The question is if a database connection should be passed in by reference or by value?</p>
  
  <p>For me I'm specifically questioning a PHP to MySQL connection, but I think it applies to all databases.</p>
  
  <p>I have heard that in PHP when you pass a variable to a function or object, that it is copied in memory and therefore uses twice as much memory immediately. I have also heard that it's only copied once changes have been made to the value (such as a key being added/removed from an array).</p>
  
  <p>In a database connection, I would think it's being changed within the function as the query could change things like the last insert id or num rows. (I guess this is another question: are things like num rows and insert id stored within the connection or an actual call is made back to the database?)</p>
  
  <p>So, does it matter memory or speed wise if the connection is passed by reference or value? Does it make a difference PHP 4 vs 5?</p>
  
  <pre><code>// $connection is resource
  function DoSomething1(&amp;$connection) { ... }
  function DoSomething2($connection) { ... }
  </code></pre>