What is PHP Memory Limit:
This directive sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. This helps prevent poorly written scripts for eating up all available memory on a server. More information about this directive is available here https://www.php.net/manual/en/ini.core.php#ini.memory-limit
The error occurs when your script requires more memory than the one specified in your php.ini file. The solution is to adjust your PHP’s directive named memory_limit. Please note this is also possible that your memory_limit value is beign ignored by PHP because of incorrect syntax. If you specify this value using the correct syntax this should solve this problem. This article also deals with some most common incorrect syntax used and the correct syntax.
memory_limit Correct Syntax:
; When an integer is used, the value is measured in bytes
memory_limit = 262144 ;
When K is suffixed after an integer value, the value is measured in Kilobytes
memory_limit = 256K ;
When M is suffixed after an integer value, the value is measured in Megabytes
memory_limit = 256M
Some Common Incorrect Syntax:
memory_limit = 128MB
memory_limit = 128KB
memory_limit = 2561442 Bytes
There are following ways to increase the value of memory_limit.
1- Edit php.ini file directly:
This solution is for those who are running a server on localhost. If your operating system is Windows, you can locate your php.ini file from this location. C:\xampp\php\php.ini. It is assumed that your xampp installation is located in C Drive on your Windows PC. If not, then locate your xampp installation on your PC and navigate to /php/php.ini. If you are using Linux, then most distributions put lampp under /opt/lampp, so the file can be found under /opt/lampp/etc/php.ini.
Open your php.ini file and then search for the term memory_limit. Change its current value to whatever you want to set. Below I am showing a portion of my php.ini file that contains this directive.
; Maximum amount of memory a script may consume
memory_limit = 128M
Note: After making changes to php.ini file, you have to restart the apache server. By this setting, the configuration is made global to all PHP scripts. Changes done to this file in an incorrect way can create problems to the web-server or live projects. To restart your apache server (if its already running first close it then) click on start button next to it. Now your changes in php.ini file should get effective.
2- Edit php.ini on remote server:
If its a remote server, then login to your Cpanel. The interface of Cpanel can be different for different hosting companies but you the basic idea is always the same. Look for PHP settings inside Cpanel dashboard, then look for the term memory_limit. Set up your desired value and save it.
3- Using the PHP built-in function:
If somehow you are not able to use any of the above methods to increase the memory_limit, you can do so using your PHP code. Place this code at the start of your PHP script so it applies before your script executes your main code.
ini_set('memory_limit', '128M'); //I have placed 128M, feel free to change this value as per your requirements
4- Using htaccess file:
This method will globally update the directive value which means all your scipts running under this htacces file will have this value.
php_value memory_limit 128M #feel free to change the value as per your requirements