Search code examples
phperror-reporting

Is there a way that I enforce that PHP reports error if I use an uninitialized/undefined variable?


I made a huge mistake by mixing result with results and it took me around 4 hours to finally find the bug.

So here is the question, in PHP, is it possible that I can enforce PHP to report errors if I use an undefined/uninitialized variable.

thank you


Solution

  • Set error reporting to E_ALL and ensure that display_errors in php.ini is on.

    php.ini

    display_errors = On
    

    PHP code

    // If you cannot access the php.ini file
    // you can do this within your PHP code instead
    @ini_set('display_errors' '1');
    error_reporting(E_ALL);
    

    The default setting you have right now probably excludes notices, the kind of errors PHP raises on uninitialized variables, which could be something like this:

    error_reporting(E_ALL & ~E_NOTICE);