5 parts · Read in order
Values and memory
What a PHP value is made of, what it costs to hold one, and when the engine decides to copy it.
What 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.
A 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.
Two 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.
A declared property is cheaper than an array key
One hundred thousand four-field records cost 143.54 bytes each as objects and 397.14 as associative arrays. Where the crossover is, and what one undeclared property costs.
Appending to a string is free until someone else holds it
The same .= statement took 0.269 ms and 218.836 ms in two loops that differ by one assignment. What a zend_string costs, where literals live, and why implode is not the fix.