Do you know that objects in PHP5 are passed by reference, while arrays and other scalar variables are passed by value? Yes, you know it, but it’s not exactly true. Let’s see some example and let’s try to answer few questions.
// depending on the machine but both lines return// expectedly equal values: 331208// 331208echomemory_get_usage();echomemory_get_usage();
// depending on the machine but both lines return
// expectedly equal values: 331208
These two lines of code, expectedly return the same value (in my case 331208), which shows us that because nothing happened in between them the memory usage isn’t growing. Let’s now put some code in between them.
Consider the following case. We have an array with identical keys.
$arr = array(1 => 10, 1 => 11);
What happens when the interpreter reaches this line of code? This is not a syntax error and it is completely valid. Very similar, but more interesting case is when we have an array of identical keys, where those identical keys are represented once as an integer and then as a string.