Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of ini_set() in php? (especially for error reporting)

Ok so PHP has the function ini_set() which a lot of people are aware of and will use to set various configuration options (here) to help with development etc. However, this function does only seem to work at runtime and will not work if there are any fatal errors or the script has syntax errors and can't be parsed / compiled.

Therefore surely there is no point of doing this (from the manual):

http://php.net/manual/en/function.ini-set.php

Examples

Example #1 Setting an ini option

<?php
echo ini_get('display_errors');

if (!ini_get('display_errors')) {
    ini_set('display_errors', '1');
}

echo ini_get('display_errors');
?>

I don't know if I'm just missing something and my php.ini isn't configured correctly, but a lot of the time I get no errors. For beginners / juniors there will no doubt be a lot of syntax errors (missing semi-colons, closing brackets etc), and said juniors would search for how to turn on errors, assume the above manual entry is correct, yet when re-running their script, alas, they get no errors as the script cannot be parsed / compiled in the first place.

I know you can set display_errors = On in the php.ini file and restart your web server to show all errors to the screen (using this in a development environment, definitely not live), but wouldn't it be better just to remove the function and only configure the php.ini file for different error levels?

Update:

I know ini_set isn't just for displaying errors, but code can't be very manageable if you're calling ini_set in certain scripts / functions / files and wouldn't it make more sense to use the php.ini for something like that?

Update

So the ini file can be used to set global configuration options, surely you'd use this for security or optimisation, however developers could still use ini_set to override some of these options at runtime which may not be desirable

In summary (@Hanky웃Panky):

Why do I have the option of displaying errors when some trivial syntax errors will still not display?

like image 864
iswinky Avatar asked Sep 10 '14 07:09

iswinky


2 Answers

yes, you are right that its better just to remove the function and only configure the php.ini file for different error levels.

But, this is good only that case when you have only one project in your machine, So, its all configuration setting you can do in php.ini Consider, the case if you have multiple project setup. if you don't want some settings in that project still it will get from php.ini

So, it is suggested for some configuration settings you just set them at project level with ini_set() and will not reflect other projects.

like image 111
Naincy Avatar answered Oct 12 '22 15:10

Naincy


 string ini_set ( string $varname , string $newvalue );

The Purpose of ini_set is to set the value of the given configuration option. This newvalue is kept by the configuration option during the script execution and restored at the scripts ending.

Example for setting an ini option

 <?php
echo ini_get('display_errors');

if (!ini_get('display_errors')) {
ini_set('display_errors', '1');
}
echo ini_get('display_errors');
?>
like image 40
Anas Nadeem Avatar answered Oct 12 '22 13:10

Anas Nadeem