Why does count() make copies of arrays?
| From: | Adam Zey | Date: | Mon, 14 Aug 2006 17:16:56 +0000 |
| Subject: | Why does count() make copies of arrays? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-240642@lists.php.net to get a copy of this message | ||
I was writing a shell script in PHP (4.4.2) that dealt with a rather large array. To figure out what I needed the new memory limit to be, I did a memory_get_usage() at the end of my script, and came up with about 5.5MB. I then set the memory limit to 8MB.
When I tried to run it, the script ran out of memory on the line:
$numwords = count($words);
However, when I switched to simply incrementing $numwords every time I added an element to $words, the memory limit of 8MB was fine.
So my question is, if PHP does copy-on-write, why does PHP make a copy of an array when you use count() on it, which should NOT be modifying the array?
Regards, Adam Zey.