56

I am getting expected notices and warnings and would like to turn them off in my PHP file. The error is:

Warning: fsockopen()

And the notice 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ossi
  • 788
  • 2
  • 14
  • 21
  • 4
    If you don't want errors e-mailed to you by cron, you can point its output at `/dev/null`. Still, errors are generally there for a reason - you'd presumably like to know when your cron script breaks! Try handling the errors gracefully. – ceejayoz Oct 29 '09 at 18:43
  • Somewhere related: http://stackoverflow.com/questions/4330494/preventing-warnings-from-fsockopen – trante Sep 07 '12 at 13:57

6 Answers6

147

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).

# Suppress 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>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pixeline
  • 17,669
  • 12
  • 84
  • 109
  • 37
    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 Oct 29 '09 at 19:31
  • 5
    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 Oct 29 '09 at 19:53
  • 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 Oct 30 '09 at 10:41
  • 7
    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 Aug 12 '10 at 17:17
  • Just wanted to add I got this error when I used `number_format()` PHP function on a value that was already formatted with `number_format()` function. – suchislife Jul 04 '20 at 14:00
20

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

More information: 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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike B
  • 31,886
  • 13
  • 87
  • 111
  • this is usually a better solution for code in production, IMHO – billrichards Mar 03 '14 at 18:33
  • 1
    Maybe, "*maybe*", for one function, however filling your scripts with this is asking for trouble later on when *something* doesn't work and your error log is empty. The best way is to set a case/switch to allow turning on and off "displaying" of errors really quickly. Then on live they're off, dev/test can be on. Either way, logging to the error log file is (nearly) always advised – James Nov 12 '14 at 20:12
14

PHP error_reporting reference:

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
Shankar Prakash G
  • 1,099
  • 18
  • 34
14

Always show errors on a testing server. Never show errors on a 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 );
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Darrell
  • 149
  • 1
  • 3
2

If you can't get to your php.ini file for some reason, disable errors to stdout (display_errors) in a .htaccess file in any directory by adding the following line:

php_flag display_errors off

additionally, you can add error logging to a file:

php_flag log_errors on

Flak DiNenno
  • 2,193
  • 4
  • 30
  • 57
2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sabeen Malik
  • 10,816
  • 4
  • 33
  • 50