If you are running a PHP script via the command line (CLI) and need to remove the memory limit, you can do it dynamically without modifying php.ini.
Command to Set Memory Limit in CLI
Run your PHP script with unlimited memory:
php -d memory_limit=-1 script.php
How It Works:
- php > Executes the PHP interpreter.
- -d memory_limit=-1 > Overrides the memory_limit setting only for this execution.
- script.php > Your PHP script.
Verify the Change in Your Script
Inside script.php, add:
<?php
echo "Memory Limit: " . ini_get('memory_limit') . PHP_EOL;
?>
echo "Memory Limit: " . ini_get('memory_limit') . PHP_EOL;
?>
Run:
php -d memory_limit=-1 script.php
Expected output:
Memory Limit: -1
Alternative: Modify PHP Script Instead
If you cannot use command-line flags, modify the script:
<?php
ini_set('memory_limit', '-1');
echo "Memory Limit: " . ini_get('memory_limit');
?>
ini_set('memory_limit', '-1');
echo "Memory Limit: " . ini_get('memory_limit');
?>
When to Use This Method
- Best for one-time executions where you need unlimited memory.
- Useful for long-running scripts (e.g., backups, large data processing).
- Works without modifying php.ini or .htaccess.
- Not recommended for web server environments (Apache/Nginx).
- Can cause excessive memory usage if not monitored.
Setting a High but Limited Memory Instead
Instead of unlimited memory, you can set a large value:
php -d memory_limit=1G script.php
or
ini_set('memory_limit', '1G');
- Use php -d memory_limit=-1 script.php for CLI execution.
- Works without modifying php.ini.
- Be cautious with unlimited memory usage in production environments.


