gpt4 book ai didi

php - 仅对部分代码禁用 PHP 警告

转载 作者:行者123 更新时间:2023-12-03 12:29:47 25 4
gpt4 key购买 nike

为了禁用 PHP 警告,您需要在 php.ini 中设置以下参数

error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = On

或者
display_errors = Off

上述方法将 禁用警告 整个项目 .有没有办法到 仅对一段 php 代码禁用警告 ,即禁用某些功能或几行代码的警告?

最佳答案

Yes , 但...

理想情况下,您希望修复这些错误(或至少以正确的方式处理它们),因为它看起来像是糟糕的设计,但是您正在寻找的被称为错误控制运算符( @ )。

<?php
/* Intentional file error */
$my_file = @file('non_existent_file') or die("Failed opening file: error was '$php_errormsg'");

// this works for any expression, not just functions:
$value = @$cache[$key];
// will not issue a notice if the index $key doesn't exist.
?>

Note: The @-operator works only on expressions. A simple rule of thumb is: if you can take the value of something, you can prepend the @ operator to it. For instance, you can prepend it to variables, function and include calls, constants, and so forth. You cannot prepend it to function or class definitions, or conditional structures such as if and foreach, and so forth.



做记录...

Warning: Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.

关于php - 仅对部分代码禁用 PHP 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45672160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com