Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using ini_set in a script considered bad practice?

Tags:

php

I am doing a code review and found 2 places where

ini_set('memory_limit', '512M');

is used in a function. Is this ok? Somehow it doesnt seem right to me. Is this considered bad practice?

Thanks!

like image 784
Arnie Avatar asked Sep 14 '25 22:09

Arnie


2 Answers

There's nothing wrong with it. Consider a scenario where memory_limit is globally set in PHP's ini file, and you want to override that setting only for one specific script/request to allow the operation to use more memory.

Calling ini_set in a PHP script will only have the effect while PHP executes that particular request.

like image 64
marekful Avatar answered Sep 17 '25 19:09

marekful


you could alternatively wrap this function call in another function that is called, making it so that you can change the implementation if a better way comes along in the future, or if you want to update the memory limit globally with little effort

function setMemory($limit = '512M') {
    ini_set('memory_limit', $limit);
}
like image 43
Logan Murphy Avatar answered Sep 17 '25 19:09

Logan Murphy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!