PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
No Result
View All Result
Home How-to

3 ways to get PHP memory limit value

February 25, 2022
Reading Time: 4 mins read
PHP101.net - Get PHP memory limit value

PHP101.net - Get PHP memory limit value

Share on FacebookShare on Twitter

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 options
  • echo ini_get('memory_limit'); (whole command in bracket) is the command to print (echo) the value of ini_get('memory_limit');, in which ini_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 values
  • grep command for only getting the memory_limit value, not every PHP configuration values.

The result will display:

[[email protected] ~]# 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:

PHP101.net - Get PHP memory limit value
The memory_limit value | PHP101.net

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:

4 ways to increase PHP memory limit

You might also like

  • 4 ways to increase PHP memory limit
  • How to write PHP comments – and best practices

References

  • PHP.net Documentation on command lines and options
  • PHP.net Documentation on phpinfo function
  • PHP.net Documentation on ini_get function
Tags: php memory limitphp memory_limit
Previous Post

4 ways to increase PHP memory limit

Next Post

How to install PHP on Windows – Standalone method

Related Posts

PHP101.net - How to set timezone with PHP

How to set timezone with PHP easily (multiple methods)

March 9, 2023
PHP101.net - How to redirect to another page using PHP

How to redirect to another page using PHP

March 8, 2023

Table of Contents

    • Quick solutions
    • What is PHP memory limit and why is it necessary?
    • How to get PHP memory limit configuration value
    • 1. Using PHP-CLI (PHP Command Line Interface)
    • 2. Using phpinfo function
    • Editing PHP memory limit setting
    • You might also like
    • References
Next Post
PHP101.net - Tutorial - Installing PHP on Windows

How to install PHP on Windows - Standalone method

We deliver free and helpful resources for learning
and using PHP programming language.

About
Contact Us
Privacy Policy
Main Sections

Home
Deploy
How-to
Tutorial
You might be interested in...

3 ways to get PHP memory limit value

How to get user IP address using PHP

How to convert from string to array in PHP

How to set MySQL charset (or MariaDB) by PHP

How to install PHP GeoIP extension & GeoLite2 library (databases included)

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone.

No Result
View All Result
  • Home
  • Deploy
  • How-to
  • Tutorial

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone. | Privacy Policy

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.