Home  >  Article  >  Backend Development  >  How to suppress error reporting in php

How to suppress error reporting in php

藏色散人
藏色散人Original
2022-11-21 10:10:201745browse

php method to suppress error reports: 1. Open the corresponding php file; 2. Use the @ operator to suppress a single error. The suppression syntax is such as "@include ('config.inc.php');"; 3. . Use the @ symbol for functions whose execution failure will not affect the overall functionality of the script.

How to suppress error reporting in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

php How to suppress error reporting?

Use @ to suppress errors

In PHP, you can use the @ operator to suppress individual errors. For example, if you don't want PHP to report that it doesn't include a certain file, you can write code like this:

@include ('config.inc.php');

Or if you don't want to see the "divide by 0" error:

$x = 8;
$y = 0;
$num = @($x/$y);

Like a function call Like mathematical operations, the @ symbol can only handle expressions. The @ symbol cannot be used before conditional statements, loop statements, function definitions, etc.

As a rule of thumb, I recommend using the @ symbol for functions whose failure will not affect the overall functionality of the script. Alternatively, you can suppress errors when you can handle PHP's errors more gracefully yourself.

Some open source software uses part of the code to suppress errors:

//code from phpbb3(common.php)  
// If we are on PHP >= 6.0.0 we do not need some code  
if (version_compare(PHP_VERSION, '6.0.0-dev', '>='))  
{  
 /** 
 * @ignore 
 */  
 define('STRIP', false);  
}  
else  
{  
 @set_magic_quotes_runtime(0);  
  
 // Be paranoid with passed vars  
 if (@ini_get('register_globals') == '1' || strtolower(@ini_get('register_globals')) == 'on' || !function_exists('ini_get'))  
 {  
  deregister_globals();  
 }  
  
 define('STRIP', (get_magic_quotes_gpc()) ? true : false);  
}  
  
//code from phpbb3(style.php)  
$dir = @opendir("{$phpbb_root_path}styles/{$theme['theme_path']}/theme");  
  
//code from phpbb3(adm/index.php)  
                    if (file_exists($phpbb_root_path . $cfg_array[$config_name]) && !@is_writable($phpbb_root_path . $cfg_array[$config_name]))  
                    {  
                        $error[] = sprintf($user->lang['DIRECTORY_NOT_WRITABLE'], $cfg_array[$config_name]);  
                    }  
  
//code from phpbb3(functions.php)  
    if (($fh = @fopen('/dev/urandom', 'rb')))  
    {  
        $random = fread($fh, $count);  
        fclose($fh);  
    }

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to suppress error reporting in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn