PHP CLI increase memory limit

By | October 3, 2009

PHP logo

When running a PHP script from command line interface (CLI), a higher memory limit may be required in order for the script to be successfully executed. This can be achieved using the “-d” or “–define” option in the command. According to the manual:

This option allows you to set a custom value for any of the configuration directives allowed in php.ini.

An example of running a PHP script in CLI with custom memory limit:

Of course the “-d” or “–define” option is not limited to the memory limit directive; it can be use to alter other php.ini directives as well.

16 thoughts on “PHP CLI increase memory limit

  1. james

    Life Saver!!!!

    I wish more people had a knack for writing things in a few short and sweet sentences.

  2. Pingback: Yii and PHP:: Executed command line and memory limit « Kongsi-Kongsi

  3. szako

    Thanks, was getting a headache already 🙂

  4. Anfa

    You forgot a ‘_’ in memory_limit in your post

  5. Pingback: Wordpress Memory Limit Increase | Fix How To

  6. Spank Bank

    If you wanted to set multiple directives, what would it look like?

  7. Cristian Radulescu Post author

    In this case you should create a custom php.ini file and pass it as option for the command. For example, create myphp.ini file and add your custom directives each on a line:
    memory_limit = 128M
    max_input_vars = 10

    Then run the command like:
    php -c myphp.ini my_script.php

  8. Robert Johnson

    Are these permanent changes to the php.ini or just temporary?

  9. Cristian Radulescu Post author

    The changes are temporary and are applied only to the executed script.
    If you want to make permanent changes you need to edit the configuration file from the server (php.ini).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.