Iterating through a massive file
Functions such as file_get_contents()
and file()
are quick and easy to use however, owing to memory limitations, they quickly cause problems when dealing with massive files. The default setting for the php.ini
memory_limit
setting is 128 megabytes. Accordingly, any file larger than this will not be loaded.
Another consideration when parsing through massive files is how quickly does your function or class method produce output? When producing user output, for example, although it might at first glance seem better to accumulate output in an array. You would then output it all at once for improved efficiency. Unfortunately, this might have an adverse impact on the user experience. It might be better to create a generator, and use the yield keyword
to produce immediate results.
How to do it...
As mentioned before, the file*
functions (that is, file_get_contents()
), are not suitable for large files. The simple reason is that these functions, at one point, have the...