title: How does PHP memory actually work
body: <p>I've always heard and searched for new php 'good writing practice', for example: It's better (for performance) to check if array key exists than search in array, but also it seems better for memory too:</p>
  
  <p>Assuming we have:</p>
  
  <pre><code>$array = array
  (
      'one'   =&gt; 1,
      'two'   =&gt; 2,
      'three' =&gt; 3,
      'four'  =&gt; 4,
  );
  </code></pre>
  
  <p>this allocates 1040 bytes of memory,</p>
  
  <p>and </p>
  
  <pre><code>$array = array
  (
      1 =&gt; 'one',
      2 =&gt; 'two',
      3 =&gt; 'three',
      4 =&gt; 'four',
  );
  </code></pre>
  
  <p>requires 1136 bytes </p>
  
  <p>I understand that the <code>key</code> and <code>value</code> surely will have different storing mechanism, but
  please can you actually point me to the principle how does it work?</p>
  
  <p><strong>Example 2 <em>(for @teuneboon)</em>:</strong> </p>
  
  <pre><code>$array = array
  (
      'one'   =&gt; '1',
      'two'   =&gt; '2',
      'three' =&gt; '3',
      'four'  =&gt; '4',
  );
  </code></pre>
  
  <p>1168 bytes</p>
  
  <pre><code>$array = array
  (
      '1' =&gt; 'one',
      '2' =&gt; 'two',
      '3' =&gt; 'three',
      '4' =&gt; 'four',
  );
  </code></pre>
  
  <p>1136 bytes</p>
  
  <p><strong>consuming same memory:</strong></p>
  
  <ul>
  <li><code>4 =&gt; 'four',</code></li>
  <li><code>'4' =&gt; 'four',</code></li>
  </ul>