What is a simple limit problem

What is the PHP memory limit and how can it be changed?

PHP is a programming language that offers optimal conditions for web applications. It is used by almost all content management systems (CMS) such as TYPO3, WordPress, Joomla or Contao and by shop systems such as Shopware and Magento. Essential settings are saved in a central configuration file php.ini, which can often be edited using the CMS or directly using a text editor. An important parameter of this file is the PHP memory limit.

How does PHP work?

PHP is one of the so-called scripting languages. This means that programs are written in easily understandable code. An interpreter then takes over the processing by reading a program line by line and executing the instructions according to its php.ini. CMS such as Magento, WordPress or Joomla consist of a large number of scripts, each of which takes on a specific task. They are loaded into a certain area of ​​the main memory, which is limited by the PHP memory limit and can be increased individually. The PHP scripts are used to create a website in HTML format from various sources such as a database, external files and special style templates - the css files. This is supplemented by user-side scripts that are written in other languages ​​such as Java or JavaScript and, for example, enable interactive operation such as games or animated menus. Here is the PHP web hosting comparison.

What is the PHP memory limit and what is its purpose?

As already mentioned, PHP reserves part of the main memory in order to be able to store and execute scripts there. The size of this area is defined by the PHP memory limit in php.ini. The limitation by the PHP memory limit protects a computer from being impaired in its performance by bad or faulty programming. A simple example of this would be a loop that creates and saves a variable whose name and value increase by 1 with each pass. Without a PHP memory limit in php.ini, this would lead to the entire working memory being occupied by this loop over time. As a result, performance drops and, under certain circumstances, the operating system itself can become unstable because there is no memory available for its data.

How high should a PHP memory limit be in productive operation?

With web hosting, the standard size of the PHP memory limit is usually between 32 MB and 64 MB. This is sufficient for the basic installation of a CMS such as WordPress, Contao, Joomla or TYPO3 and allows stable operation. Complex plugins, large databases or many extensions mean that more memory is required for the scripts and their data. In this case, PHP only outputs an error message instead of a website, which says, for example:

Fatal error: Allowed memory size of XXXXXX bytes exhausted (tried to allocate XXXXXX bytes) in /var/www/html/skript.php

In this case it is necessary to increase the PHP memory limit so that the interpreter has sufficient temporary memory available for the execution of all scripts. Recommended limits for a CMS are between 32 MB (WordPress) and 256 MB (Shopware) - more than 512 MB is rarely necessary and often indicates programming problems.

How can the PHP memory limit be increased?

Most web hosts allow their customers to increase the PHP memory limit within certain limits. There are various ways of doing this, which are not all offered by every Internet service provider at the same time:

  • Method 1: change in php.ini

    The php.ini actually contains the "global" configuration of the PHP interpreter - this means that changes in the php.ini apply in principle to the entire server. However, some providers also allow the creation of an individual php.ini in which their customers can make local settings. For the most part, php.ini consists of comments that document the file and its functions in a very comprehensive and clear manner. It contains a section on Resource Limits, in which, in addition to the maximum runtime of a script, the PHP memory limit can also be increased. It will come with the entry

    memory_limit = 64M

    fixed - here it is 64 MB. With their own root server or VPS, customers are of course free to increase the PHP memory limit as desired. In the case of web space, the provider will of course usually forbid its customers to change global configurations (which also affect other users).
  • Method 2: a .htaccess file
    In addition to the php.ini, the PHP memory limit can also be increased in a .htaccess file. This is in the lowest directory of WordPress, TYPO3, Contao or other CMS and contains instructions that are read and used before executing a script. The decisive restriction is that .htaccess is only supported by an Apache 2 web server. To increase the PHP memory limit, it is sufficient to create a file with the name .htaccess and the content

    php_value memory_limit 64M
  • Method 3 (WordPress): Definition in config.php
    Some CMS like WordPress allow you to increase the PHP memory limit in your local configuration file. To do this, the file Installation / wp-config.php (WordPress) must be opened and the following new definition must be inserted

    define ('WP_MEMORY_LIMIT', '64M');

It should be noted that every web space and every Internet service provider restricts the maximum PHP memory limit of its customers in order to control memory consumption. The maximum permissible limit is usually between 128 and 512 MB, depending on the provider. Server monitoring should be a must-have for the professional operation of a website.

Photo: Boskampi | pixabay.com