Knowing what PHP memory limit value is will help in configuring and optimize for your PHP scripts to run efficiently. This article will guide you on how to quickly get PHP memory limit configuration value, and more in-depth guides on how the methods work.
Quick solutions
1. By command line: Run this command on SSH or Windows command line:
php -r "echo ini_get('memory_limit');"
The command result will show the memory limit configuration value:
512M
In this case, the PHP memory limit value is 512 MB.
2. By phpinfo
: Create a new PHP file with this line:
<?php phpinfo();
Save the file, open it on the browser, and look for memory_limit
, which will show the current setting value.
Above are quick solutions for getting the setting value of PHP memory limit. To know more details on PHP memory limit, and the processes to get its value, you can continue with the article within the below sections.
What is PHP memory limit and why is it necessary?
A PHP script needs memory assigned to it for it to be running properly. As in PHP.net, it is the maximum amount of memory in bytes that a script is allowed to allocate. This helps poorly written PHP scripts won’t consume too much server memory, as well as malicious PHP scripts that always eat up the server bandwidth.
Too low memory limit setting value can cause issues on the PHP scripts needing fair memory allocated for it to run properly. In general, 256MB (256M in PHP memory_limit
syntax) is more than enough for most PHP scripts to run properly.
How to get PHP memory limit configuration value
There are multiple ways to get PHP memory limit value, here we will introduce the most common methods along with explanation on how they work.
1. Using PHP-CLI (PHP Command Line Interface)
1.A. Using SSH terminal (in Linux based operating systems) or Windows command line, we can use this command to get the PHP memory limit value and print it to the terminal:
php -r "echo ini_get('memory_limit');"
This command can be used in both Windows and Linux based operating systems.
In this command:
php
is the required command to execute a PHP command in terminal-r
is required for running commands without the script tags (<?php ... ?>
), as defined in PHP.net Command line optionsecho ini_get('memory_limit');
(whole command in bracket) is the command to print (echo
) the value ofini_get('memory_limit');
, in whichini_get
is the function to get PHP configuration value.
The result will simply display the configuration value, here is 512M, which is 512 MB in PHP configuration syntax:
512M
1.B. Using SSH terminal in Linux based operating systems only, type this command:
php -i | grep "memory_limit"
In this command:
-i
is for getting PHP configuration valuesgrep
command for only getting thememory_limit
value, not every PHP configuration values.
The result will display:
[root@php101dotnet ~]# php -i | grep "memory_limit"
memory_limit => 512M => 512M
This will show two values for PHP memory limit configuration, which the first one is the local value set in the current web directory, the second one is the master value defined in php.ini (system-wide).
2. Using phpinfo
function
We can create a php file with the phpinfo()
function to get the memory limit setting:
<?php phpinfo();
Open the file on browser, and look for memory_limit
value will show the setting we need:
Editing PHP memory limit setting
When you can get PHP memory limit value, the next step is adjust it properly.
We have a detailed article on how to edit PHP memory limit configuration values: