Values and memory

What a PHP value is made of, what it costs to hold one, and when the engine decides to copy it.

3 parts · Read in order

  1. 1What a PHP variable actually is

    A zval is sixteen bytes, a value union and a type tag. What that tag says decides whether an assignment copies anything, what a reference costs, and which variable pays.

  2. 2A by-reference foreach leaves a reference in every element

    Walking an array with foreach ($rows as &$row) wraps every element in a zend_reference. On a million integers that is 32 MB the loop never gives back.

  3. 3Two hundred thousand objects, and the collector never ran

    Two hundred thousand objects created and dropped, and zero collections. What puts an object in the root buffer, and why the collector's cost is set by what you keep alive.

Arrow keys to move, Enter to open.