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

5

8

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?

Muscadine answered 10/9, 2014 at 7:46 Comment(5)
ini_set('display_errors', true);Forbes
ini_set is not only for setting error display mode :)Shrewish
@Hanky웃Panky yeah I understand that, see updateMuscadine
That being said, it is a valid question to wonder why do I have the option of displaying errors when some trivial syntax errors will still not displayShrewish
This might help #845521Shrewish
T
2

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.

Titlark answered 10/9, 2014 at 7:53 Comment(0)
C
1

string ini_set ( string $varname , string $newvalue )

Basically 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.

for all the variables which you can configure during the script run. please go through the below link.

Chinch answered 18/1, 2018 at 7:49 Comment(0)
S
1
 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');
?>
Spread answered 9/7, 2018 at 9:10 Comment(0)
P
0

Another settings can be configured at runtime using the the ini_set() function: memory_limit and max_execution_time (From ZCE test part about PHP Basics).

Pizor answered 16/7, 2019 at 17:0 Comment(0)
H
-1

ini_set — Sets the value of a configuration option. 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, without ini_set(), values from php.ini file will be used.

EDIT:

You may find this helpful:

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
Hanford answered 10/9, 2014 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.