Increase PHP memory limit with ini_set

There is a mobile optimized version of this page, view AMP Version.

As a developer you will constantly need to increase the memory that is allowed for PHP, you will usually find that this is essential when reading and/or writing large files or when working with large data sets. Luckily there is a very easy way yo increase the amount of memory allowed to PHP so that your script can do what is necessary without running out of memory.

ini_set('memory_limit', '50M');

But for those like me who like to optimize their scripts to use as little memory as possible, you may be wondering "how can I optimize my PHP script so that it uses as little memory as possible?", well one of the biggest things you can do is clear variables once you have finished working with them, this is especially important when working in loops.

All you need to do is set variables to NULL once you have finished with them, this will clear any data in memory associated with the variable.

$myvar = null;

TIP: If you're working with large files of data, don't be afraid to process the data on the fly into a multi-dimensional array, get the data into a clean efficient array ready suitable for whatever you need later in the script, this means you will not be keeping large sums of irrelevant data in memory for more than a short period.

Author: Dean Williams

I'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips