2

I have a simple question, should I write setting or php.ini file or use ini_set() on php file. Which is better regarding the performance?

Thank you very much.

Micah
  • 4,254
  • 8
  • 30
  • 38

3 Answers3

7

Depends on your aim. php.ini settings will be applied for all php applications by default, ini_set will change php settings on the fly for the current php application.

If you have performance question, php.ini should be faster, because you skip function call of ini_set.

Viacheslav Kondratiuk
  • 8,493
  • 9
  • 49
  • 81
3

On typical 2018 hardware running ini_set('date.timezone', $timezone) 1,000,000 times takes about 500ms.

For PHP 7.1, ini_set() is definitely not slower than loading another .ini file. In fact, it is faster to run ini_set() in a second file with require(), than to load an .ini file.

Even an empty .ini file has some overhead. I suspect internally the .ini files do something similar to ini_set anyway.

# Baseline (no .ini, empty script)
$ time for ((i=0;i<1000;i++)); do php -n empty.php; done
real    0m21.214s

# php.ini: date.timezone = Australia/Sydney
$ time for ((i=0;i<1000;i++)); do php -c php.ini empty.php; done
real    0m23.015s

# inline.php: <?php ini_set('date.timezone', 'Australia/Sydney') 
$ time for ((i=0;i<1000;i++)); do php -n inline.php; done
real    0m21.150s

These numbers seem to be pretty representative. However, the performance difference here is not that meaningful. It probably comes down to what sort of environment consistency and flexibility you need.

Ryan
  • 4,594
  • 1
  • 32
  • 35
2

As I think ini_set() is better approach than open your ini file and doing changes because if you change the ini setting once than it will be applicable on you all php projects but in ini_set() you dont have to even think about your other php application.