簡體   English   中英

PHP關閉錯誤 - 僅在一個文件中

[英]PHP turn off errors - in one file only

我很清楚error_reporting(0); ini_set('display_errors', "Off"); 使錯誤消息消失。

什么是適當的方法 - 僅針對特定文件或部分代碼? @s的壓制錯誤似乎是一個壞主意,因為它顯然減慢了代碼的速度......

原因? 我們在開發局域網中有許多memcached服務器由於網絡設置而真的不可靠,因此我們每小時都會多次收到錯誤,除了停止使用內存緩存或關閉整個應用程序的錯誤外,我們無能為力,這將讓我們頭疼 - 在開發階段的中間:)

<?php

    // normal code

    // error_reporting returns the old error code
    $old_error_reporting = error_reporting(0);

    // your errorful code

    // reset error_reporting to its old value
    error_reporting($old_error_reporting);

    // normal code

雖然修復實際導致錯誤的內容是個好主意。

你有點回答了自己的問題。 要為特定文件執行此操作,error_reporting(0); 將關閉錯誤。 我想你也可以在腳本中多次調用它。

您還可以使用php異常來“捕獲”代碼塊上的錯誤。 例如:

try {
    // code to ignore errors for here
} catch {
    // you can output a custom error here, but putting nothing here will effectively mean errors are ignored for the try block
}

腳本將繼續運行通過try塊,即使其中存在錯誤。 有關更多信息,請參閱PHP手冊條目

您可以在運行時更改錯誤報告級別:

<?

   error_reporting(E_ALL);

   ... some code ....

   error_reporting(0);

   ... some more code ....

   error_reporting(E_ALL);

我不知道別的辦法,但我想不出這樣的情況還不夠。 你是否可以?

那真的很久以前,但像我這樣的人可能會用我的答案。

當我需要做這種事情時,我只是在變量前加上@,以便不顯示來自這個變量的錯誤。

例:

switch(@$var!="e") {

....

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM