I\'ve just found something very strange in PHP.
If I pass in a variable to a function by reference, and then call a function on it, it\'s incredibly
I found a bug report from 2005 that describes exactly this issue: http://bugs.php.net/bug.php?id=34540
So the problem seems to be that when passing a referenced value to a function that doesn't accept a reference, PHP needs to copy it.
This can be demonstrated with this test code:
This runs quickly, but if you change function CalledFunc(&$aData) to function CalledFunc($aData) you'll see a similar slow-down to the count example.
This is rather worrying, since I've been coding PHP for quite a while and I had no idea about this issue.
Fortunately there's a simple workaround that is applicable in many cases - use a temporary local variable inside the loop, and copy to the reference variable at the end.