tags:

views:

1566

answers:

4

I am getting expected notices and warnings, and would like to turn them off on my php file. errors are:

Warning: fsockopen()

and notices are:

Notice: A non well formed numeric value encountered in

I am planning to use cron for this php script, and do not want to get any errors or notices logged anywhere... ie cpanel error log etc...

Thanks

+2  A: 

When you are sure your script is perfectly working, you can get rid of Warning and notices like this: Put this line at the beginning of your php script:

error_reporting(E_ERROR);

Before that, when working on your script, i would advise you to properly debug your script so that all notice or warning disappear one by one. So you should first set it as verbose as possible with:

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

UPDATE : how to log errors instead of displaying them As suggested in the comments, the better solution is to log errors into a file so only the php developer sees the error messages, not the users. A possible implementation is via the .htaccess file, useful if you don't have access to the php.ini file (source).

# supress php errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_value docref_root 0
php_value docref_ext 0
# enable PHP error logging
php_flag  log_errors on
php_value error_log  /home/path/public_html/domain/PHP_errors.log
# prevent access to PHP error log
<Files PHP_errors.log>
 Order allow,deny
 Deny from all
 Satisfy All
</Files>
pixeline
Don't do this. Suppressing all errors is a horrid idea.
ceejayoz
you're speaking as if reciting a mantra. In this specific use case, that's exactly what the user wants. If he'd asked for a logging system, he'd ask for it.
pixeline
I agree with both ceejayoz and pixeline. However, error_reporting(0) should only be set as a security measure on production environments so critical information is not mistakenly leaked to malicious users. Using error_reporting(0) to 'ignore errors' carries a high risk of leading to bad things. I know it's what the author asked for, but I would suggest against it.
Mike B
ok, i'll amend so that it suppresses warning and notices. that should be enough. Let's agree that errors should be reported, but notices and warning don't need to, once his script is properly working.
pixeline
or we could actually point out that there are facilities to suppress the display of errors on stdout but point them to a log.
Adriano Varoli Piazza
true. Answer updated accordingly. Thanks.
pixeline
+2  A: 

you can set the type of error reporting you need in php.ini or by using error_reporting() function on top of your script.

Sabeen Malik
+3  A: 

Prepend functions with the '@' symbol to suppress certain errors, As opposed to turning off ALL error reporting.

More Info: http://php.net/manual/en/language.operators.errorcontrol.php

PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.

@fsockopen();

Mike B
A: 

Always show errors on testing server. Never show errors on production server.

Write a script to determine whether the page is on a local, testing, or live server, and set $state to "local", "testing", or "live". Then:

if( $state == "local" || $state == "testing" )
{
 ini_set( "display_errors", "1" );
 error_reporting( E_ALL & ~E_NOTICE );
}
else
{
 error_reporting( 0 );
}
Darrell