I am writing a script to change php.ini automatically through a series of replacements. The replacements are done by regex, so the script is overly complicated. (An example: I change the error_reporting line to error_reporting = E_ALL).
Two questions:
1 -
If a setting is set multiple times in php.ini, which one takes precedence, the first or the last?
display_errors = On
display_errors = Off
Which one will be used by php.ini? Or will I get an error and php won’t start?
2 - What is the best practice way to change php.ini? I am just using a shell script now, but I am hoping a better solution might exist.
Tried to edit my post, but was too late. On Question 1, the reason why I am curious is because I would like to just lump all of my changes to the bottom of the file, and leave the original setting intact, if possible.
$orignal_value = ini_get('whatever');
ini_set('whatever', $new_value);
// do stuff while ini_set is new value
ini_set('whatever', $orignal_value);
// do stuff while ini_set is original value
I’ve had to but only very rarely.
eg. setting timezone and throwing more memory and time at a heavy script.
For example, if you moved displayErrors = off from the top of the file to the bottom of the - and you only have ONE instance - would things still work as expected?
(Personally, I find the php.ini to be unwieldy in how it is documented and organized!!)
AFAIK PHP runs the code as it gets to it. So you could change things around. eg.
$var = $massive_memory_hogging_array;
$results = process ($var);
echo $results;
unset($var); // free up memory
// continue with the script
I’m not sure how PHP does GC, there seems to be some debate as to the “when it happens”, but in any case I’ve only done it for very long resource intensive scripts.
And yes, “ini” could be better. Some are not configurable with ini_set, some take boolean, some take Ints, some take “mixed”.
Thank goodness the documentation is there, I’d never remember all of the variations
I guess I wasn’t very clear, sorry - I am not changing php ini settings during a php script, but rather editing the php.ini, mostly by a shell script (but also php scripts) so that the php.ini is setup exactly the way I want it in each development environment I make. I hate having to copy and paste php.inis, especially since they are different for each environment I have, so I’d rather write a script I can edit and run anytime I need to setup the php.ini in a certain per-defined way (development on ubuntu vs other distros or windows, staging, production, and so on).
The goal is to bypass any given php.ini settings and just tack on the things I want to change to the bottom of the php.ini. So it may say display_errors = Off on line 75, but on line 250 it says display_errors = On and so php counts the latter setting as what is used and ignores the former setting on line 75. I am worried this may result in errors (or not even work), so that is why I asked.
I thought php’s ini_set was a temporary function that ran only for the duration of a script, with each php script falling back onto the default and hard-coded php.ini when running for the first time. Can ini_set be used to make permanent changes to the php.ini? That would be exactly what I want if I could do that and I wouldn’t even have to worry about order of interpretation in php.ini’s settings. But the php.net manual says that ini_set “Sets the value of the given configuration option. The configuration option will keep this new value during the script’s execution, and will be restored at the script’s ending.” Can it somehow be used to permanently alter the php.ini?