dongran1779 2017-08-14 10:09
浏览 52
已采纳

仅为部分代码禁用PHP警告

In order to disable PHP Warnings, you will need to set the following parameters in php.ini

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

or

display_errors = Off

The above method will disable warnings for the whole project. Is there a way to disable warnings for only a block of php code, i.e. disable warnings for certain functions or few lines of code?

  • 写回答

2条回答 默认 最新

  • dongshen2903 2017-08-14 10:10
    关注

    Yes, but...

    ideally you'd want to fix those errors (or at least handle them in a correct manner) as it just seems like bad design however what you're looking for is known as the error control operator (@).

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

    Take note...

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀