php php中ini_set()的目的是什么?(尤其是错误报告)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25759947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:59:12  来源:igfitidea点击:

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

phpcompiler-errorsini

提问by iswinky

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.

好的,所以 PHP 具有ini_set()很多人都知道的功能,并将用于设置各种配置选项(此处)以帮助开发等。但是,此功能似乎只在运行时有效,如果有的话将无法使用致命错误或脚本有语法错误且无法解析/编译。

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

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

例子

Example #1 设置一个 ini 选项

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

我不知道我是否只是遗漏了一些东西,我的 php.ini 没有正确配置,但很多时候我没有错误。对于初学者/初级者来说无疑会有很多语法错误(缺少分号,右括号等),并且说初级者会搜索如何打开错误,假设上面的手动输入是正确的,但重新运行时他们的脚本,唉,他们没有错误,因为脚本首先无法解析/编译。

I know you can set display_errors = Onin 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.inifile for different error levels?

我知道您可以display_errors = On在 php.ini 文件中进行设置并重新启动 Web 服务器以将所有错误显示在屏幕上(在开发环境中使用它,绝对不是实时的),但是仅删除该功能并且仅删除该功能不是更好吗?为不同的错误级别配置php.ini文件?

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?

我知道 ini_set 不只是用于显示错误,但是如果您在某些脚本/函数/文件中调用 ini_set 代码就不是很容易管理,并且使用 php.ini 来处理类似的事情不是更有意义吗? ?

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

因此 ini 文件可用于设置全局配置选项,您肯定会使用它来进行安全或优化,但是开发人员仍然可以使用 ini_set 在运行时覆盖其中一些可能不理想的选项

In summary(@Hanky?Panky):

总之(@Hanky?Panky):

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

为什么在一些微不足道的语法错误仍然不会显示时我可以选择显示错误?

回答by Naincy

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

是的,您是对的,最好删除该功能并仅针对不同的错误级别配置 php.ini 文件。

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

但是,这仅适用于您的机器中只有一个项目的情况,因此,您可以在 php.ini 中进行所有配置设置 考虑一下,如果您有多个项目设置的情况。如果您不想在该项目中进行某些设置,它仍然会从 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.

因此,对于某些配置设置,建议您仅使用 ini_set() 在项目级别设置它们,而不会反映其他项目。

回答by Shashank Agarwal

string ini_set ( string $varname , string $newvalue )

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.

基本上ini_set()设置给定配置选项的值。配置选项将在脚本执行期间保留此新值,并在脚本结束时恢复。

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

对于您可以在脚本运行期间配置的所有变量。请通过以下链接。

回答by Anas Nadeem

 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.

ini_set 的目的是设置给定配置选项的值。这个新值在脚本执行期间由配置选项保留,并在脚本结束时恢复。

Example for setting an ini option

设置 ini 选项的示例

 <?php
echo ini_get('display_errors');

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

回答by Eli

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

可以在运行时使用 ini_set() 函数配置另一个设置:memory_limit 和 max_execution_time(来自 ZCE 测试部分关于 PHP 基础知识)。

回答by Pavol Havlik

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.

ini_set — 设置配置选项的值。设置给定配置选项的值。配置选项将在脚本执行期间保留这个新值,并在脚本结束时恢复,如果没有 ini_set(),将使用来自 php.ini 文件的值。

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);