How to correctly handle errors in PHP via phplint
I'm very new to PHP. I have a line of code on a server side script that opens a file:
$fh = fopen($myFile, 'r');
When I run this code through phplint, it gives me the notice:
$fh = fopen($myFile, 'r');
\_ HERE
==== 19: notice: unhandled error(s): E_WARNING
I tried creating an error handler earlier and specifically setting it to handle E_WARNING
like this:
set_error_handler("errorHandler", E_WARNING);
But I get the same notice. I'd like to do th开发者_JAVA百科is correctly. Is there some other mechanism to handle this error that phplint
thinks is correct?
The PHPLint warning indicates that the function (fopen in this case) is documented as "raising" an E_WARNING if an error condition occurs.
If this occurs, then odds are that you will get an error message displayed in the console or on screen. Most programmers don't want this in production code and will silence the error by adding @ to suppress this warning or changing the php.ini (error_reporting).
PHPLint is just reminding you that you did not add the @. Lint would like your code to be more like:
$fh = @fopen($myFile, 'r');
// check $fh here
Obviously adding a file_exists() or is_readable() before the call would not silence PHPLint.
Ref: http://www.icosaedro.it/phplint/manual.html?p=errors
Ref: http://php.net/manual/en/function.fopen.php "If the open fails, an error of level E_WARNING is generated. You may use @ to suppress this warning."
You can use PHP's is_readable()
method to see whether the file exists and is readable before reading from the file.
Manual entry : http://php.net/manual/en/function.is-readable.php
You can silence the error using @fopen
, and then check to see if the return value === FALSE
instead.
精彩评论