Search code examples
phperror-reporting

on what does the error_reporting directive impacts?


from what I have read this directive specifies what type of errors will be reported to the script output (if display errors is on) or will be reported to the error_logs if log_errors is ON.

why in the following script I set the error_reporting to 0 ( turns off error_reporting ) and still got the message that describes the fatal error?

<?php
error_reporting(0) ; 

function a(&$name)
{

}

a("gg") ; // cause a fatal error, and display the message 

echo "gg" ;

?>

Error is:

Fatal error: Only variables can be passed by reference


Solution

  • Since error_reporting is a function call, it has to be executed so it can take effect. But before anything in the script can be executed, the entire script has to be parsed. If there's a parse error, nothing runs, so it can't change the error reporting. So parse errors are always reported according to the error reporting setting in php.ini.

    In the case of your script, when a function is declared to take a reference parameter, that changes how calls to the function are parsed. Instead of allowing any expression as the argument, it requires it to have the syntax of a variable. Since "gg" is not a variable, you get a fatal error during parsing.

    error_reporting controls reporting of errors that can only be detected at runtime, usually due to the values of variables. As an example:

    $numerator = 1;
    $denominator = 0;
    
    error_reporting(E_ALL); 
    echo "WIth error reporting: ";
    echo $numerator/$denominator;
    echo "<br>";
    
    error_reporting(0);
    
    echo "WIthout error reporting: ";
    echo $numerator/$denominator;
    echo "<br>";
    

    This will report an error for the first division, but not the second:

    WIth error reporting: 
    Warning: Division by zero in /private/var/folders/ts/tsRTg2iv2RW88k+8ZOgz7++++TY/-Tmp-/1B4537F5-61D1-487A-9062-9204D7A058D3 on line 9
    
    WIthout error reporting: