set_error_handle 和 at(@)符号的有关问题
set_error_handle 和 at(@)符号的问题
问题是这样的,我用set_error_handle 将一些系统错误或者警告信息写入到日志文件中。
但是对于一些api 比如fopen之类的,如果文件不存在的情况下也会跑到 自己写的错误处理函数中,之前是在fopen之前加上@符号,对诸如此类问题不进行显示。
如果我想在使用set_error_handle的情况下还能不把@的功能去掉。怎么做啊?
谢谢大侠们了
------解决方案--------------------
用try catch捕获
------解决方案--------------------
可以去掉,使用set_error_handler你只需要注意两点:
1.set_error_handler只会捕获error_reporting里定义的错误类型,这个可以在php.ini里配置,也可以使用函数设置;
2.自定义的错误处理函数返回值如果为false,则会触发php的默认错误处理。而返回值为true则不会触发php默认的错误处理,则看不到那个讨厌的报错了。
至于写法你可以参考一下。
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
//如果这个错误类型没有包含在error_reporting里
return;
}
switch ($errno) {
case E_USER_ERROR:
echo "My ERROR [$errno] $errstr
\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")
\n";
echo "Aborting...
\n";
exit(1);
break;
case E_USER_WARNING:
echo "My WARNING [$errno] $errstr
\n";
break;
case E_USER_NOTICE:
echo "My NOTICE [$errno] $errstr
\n";
break;
default:
echo "Unknown error type: [$errno] $errstr
\n";
break;
}
//返回false则会触发php的默认错误处理,反回true则只用自定义函数处理。
return true;
}
set_error_handler('myErrorHandler');
fopen('/tmp/foo', 'r');
------解决方案--------------------
经测试,#5的代码可以满足你的要求!
执行后,若
fopen('/tmp/foo', 'r');
失败,则显示
Unknown error type: [2] fopen(/tmp/foo) [function.fopen]: failed to open stream: No such file or directory
若
@fopen('/tmp/foo', 'r');
失败,则无任何显示

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





PHP is a popular web development language that has been used for a long time. The PDO (PHP Data Object) class integrated in PHP is a common way for us to interact with the database during the development of web applications. However, a problem that some PHP developers often encounter is that when using the PDO class to interact with the database, they receive an error like this: PHPFatalerror:CalltoundefinedmethodPDO::prep

It is very common to use axios in Vue applications. axios is a Promise-based HTTP client that can be used in browsers and Node.js. During the development process, the error message "Uncaught(inpromise)Error: Requestfailedwithstatuscode500" sometimes appears. For developers, this error message may be difficult to understand and solve. This article will explore this

Solve the "error:incompletetypeisnotallowed" problem in C++ code. During the C++ programming process, you sometimes encounter some compilation errors. One of the common errors is "error:incompletetypeisnotallowed". This error is usually caused by operating on an incomplete type. This article will explain the cause of this error and provide several solutions. firstly, I

Solve the "error:expectedinitializerbefore'datatype'" problem in C++ code. In C++ programming, sometimes we encounter some compilation errors when writing code. One of the common errors is "error:expectedinitializerbefore'datatype'". This error usually occurs in a variable declaration or function definition and may cause the program to fail to compile correctly or

Solution to "0271: real time clock error" that cannot boot: 1. Press F1, and in the interface that appears, move the option bar to the third item "Date/Time"; 2. Manually change the system time to the current one time; 3. Press F10 and select yes in the pop-up dialog box; 4. Re-open the notebook to boot normally.

How to solve PHPWarning:fopen():SSLoperationfailedinfile.phponlineX In PHP programming, we often use the fopen function to open files or URLs and perform related operations. However, when using the fopen function, sometimes you will encounter something similar to Warning:fopen():SSLoperationfailedinfile.p

How to solve PHPWarning:fopen():failedtoopenstream:Permissiondenied In the process of developing PHP programs, we often encounter some error messages, such as PHPWarning:fopen():failedtoopenstream:Permissiondenied. This error is usually due to incorrect file or directory permissions

When writing web applications using PHP, a MySQL database is often used to store data. PHP provides a way to interact with the MySQL database called MySQLi. However, sometimes when using MySQLi, you will encounter an error message, as shown below: PHPFatalerror:Calltoundefinedfunctionmysqli_connect() This error message means that PHP cannot find my
