使用 php.ini 文件设置

关闭通知的最简单、最方便的方法是通过 php.ini 文件设置。
我们需要做的就是搜索代码行 error_reporting。
其中我们将看到一行默认值:E_ALL。

我们需要将其替换为默认值: E_ALL & ~E_NOTICE(screenshot) 。

它会显示除通知之外的所有错误。
确保已启用该部分,然后刷新或者重新启动服务器。

将代码添加到 PHP 文件

其中我们将熟悉另一种关闭 PHP 通知的方法。
我们只需在文件代码的开头添加一行代码即可。
例如,wdd.php。

代码将如下所示:

<?php
//Open a file 
$file = fopen("wdd.txt", "w") or die("Unable to open file!");
//Storing the string into a variable 
$txt = "onitroad \n";
//Write the text content to the file 
fwrite($file, $txt);
//Storing the string into variable 
$txt = "Welcome to onitroad! \n";
//Writing the text content to the file 
fwrite($file, $txt);
//Closing the file 
fclose($file);
?>

我们应该在代码的开头添加以下行:

error_reporting(E_ERROR | E_WARNING | E_PARSE);

这是它的样子:

<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
//Open a file 
$file = fopen("wdd.txt", "w") or die("Unable to open file!");
//Storing the string into a variable 
$txt = "onitroad \n";
//Writing the text content to the file 
fwrite($file, $txt);
//Storing the string into variable 
$txt = "Welcome to onitroad! \n";
//Writing the text content to the file 
fwrite($file, $txt);
//Closing the file 
fclose($file);
?>

运行上面的代码后,我们将看到警告、错误以及编译时解析错误。

如何在 PHP 中关闭Notice通知

在 PHP 项目中的特定行或者一组行中指定的未定义变量称为通知。

通常,它们不会破坏代码功能。

当 PHP 注意到检测到的错误时,会显示以下通知:

PHP Notice: Use of undefined constant name - assumed 'name' in line number

但是,有时开发人员想要关闭此类通知。
其中我们将展示如何应对此类情况。

将@ 添加到运算符

如果我们想让 PHP 通知在正在进行的操作期间保持静音,则将 @ 添加到操作符中,如下所示:@$mid= $_POST[‘mid’];

PHP 中的Notice说明

在 PHP 中,通知Notice 被称为“软错误”。
在 php.ini 配置中启用错误日志记录(与调试相同)后,它们会在不同开发阶段的日志中弹出。

有时,开发人员发现通知和警告相同。
然而,它们是不同的:注释是一种建议性消息,而警告则告诉我们做错了什么,这可能导致进一步的错误。

但是,我们建议认真对待通知和警告。

日期:2020-06-02 22:15:54 来源:oir作者:oir