1

I am looking for best solution how to disable known warnings (and irrelevant warnings for my script) reported from an included file.

Short example of the included file:

$ cat incl_file.php 
<?php
error_reporting(E_ALL);
ini_set("display_errors", "on");


$x = $y;

?>

Example of the desired code (which doesn't prevent displaying errors from the included file)

$ cat main2.php 
<?php
error_reporting(E_ALL);
ini_set("display_errors", "on");

@include_once "incl_file.php";

$d=$e;

print "main_file\n";
?>

The output:

$ php main2.php 

Notice: Undefined variable: y in /tmp/php_hack/incl_file.php on line 6

Notice: Undefined variable: e in /tmp/php_hack/main2.php on line 7
main_file

The Following "workaround" works but I'm not satisfied with it's mess:

<?php

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    if (!(error_reporting() & $errno)) {
        // This error code is not included in error_reporting
        return;
    }
    //print "called $errstr\n";
    /* Don't execute PHP internal error handler */
    return true;
}

set_error_handler("myErrorHandler");
error_reporting(0);
ini_set("display_errors", "off");

include_once "incl_file.php";

restore_error_handler();

error_reporting(E_ALL);
ini_set("display_errors", "on");

$d=$e;

print "main_file\n";
?>

Output:

$ php main.php 

Notice: Undefined variable: e in /tmp/php_hack/main.php on line 24
main_file

If the included file doesn't have

error_reporting(E_ALL);
ini_set("display_errors", "on");

then the @ operator works as expected...

Jonathan
  • 6,741
  • 7
  • 52
  • 69
  • The error is quite obvious? Just set `$y` in your included file (as well as possibly `$e`? That said, don't use the `@` operator to remove notices/warnings. It's a bad habbit, and it makes it harder to figure out where errors happen. – h2ooooooo Sep 20 '13 at 09:18
  • The `@` operator just means it won't output errors as a result of the actual `include` such as an error that the included file couldn't be found etc. It doesn't turn off errors for all code included. – Anthony Sep 20 '13 at 09:20
  • I just used $x = $y; to trigger php error. I could use trigger_error() instead. – Alojzij Blatnik Sep 20 '13 at 09:44

4 Answers4

3

@ operator works only for expression with which it was used. It can't disable error reporting for included file. I'll just hide warning when PHP can't include file.

Elon Than
  • 9,603
  • 4
  • 27
  • 37
  • 1
    Thanks. But @ actually hides errors if included file don't turns error_reporting back to E_ALL. But I see, that using @ is really bad idea. – Alojzij Blatnik Sep 20 '13 at 09:49
  • @Predkambrij I just checked it, strange, but you're definitely right. Only exception is when you call function/method in parent file, then all errors/notices from this call will be displayed. – Elon Than Sep 20 '13 at 10:28
1

The error suppression operator @ can be used on operators, conditions and even on language constructs. However, whilst it can be used on function or method calls, it cannot be used when declaring functions or classes.

It just hides the errors but the errors are still there!

See this example.

<?php
@require_once("xxccc.php"); // FATAL error occurred (error won't be printed!)
                            // and program exits without error since the error
                            // suppression operator.
echo "Hello"; // This won't be printed on the screen.
halfer
  • 19,824
  • 17
  • 99
  • 186
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

It's generally not a good idea to disable warnings. You can fix your Undefined Variable notices by using Ternary Operators like so:

$variable = isset($variable) ? $variable : '';

If you put that after the code that is supposed to set the variable, and before using the variable, it will define if it is not already defined.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jadenn
  • 3
  • 3
0

You can suppress errors in many ways, if you dont want php, for example in .htacces using this:

php_flag display_errors off

Anyway @ is bad practice, and also setting different error reporting in each file

You should have some config file, where you state what errors you want to see or not, and offcourse when on release, dont show them, it could be the security leak, blank page is still better.

if($state == "testing" )
{
 ini_set( "display_errors", "1" );
 error_reporting( E_ALL & ~E_NOTICE );
}
else
{
 error_reporting( 0 );
} 
JTC
  • 3,344
  • 3
  • 28
  • 46