Just like any other programming language PHP comes with varying levels of error reporting. For those of you familiar with compiled programming languages(eg: Java, C, C++, etc…) PHP’s error reporting is different than that found in compiled languages. All the errors you receiver will present themselves at different points when running the PHP script.
PHP Notices & Warnings
Notices and warnings in PHP do not stop execution of the script. Notices are just that, notices from the interpreter about a particular line of code that did not hold an expected value. This often happens when we try to use variables which have not been declared or array indices which have not been defined.
For example: We might be declaring a variable or array index ONLY when a particular condition is true — inside and if-else statement — and then trying to use the variable.
In both of these cases, we would be trying to access the value of an undefined item.
To fix notices, we would simply make sure that the expected variable was declared, by making sure the logic of the script was sound — correct and valid.
It is entirely possible to turn off notices from appearing when the script executes. Although I would strongly suggest that scripts should be written in a way that ensures that they are free of errors. In the section below I will cover how error reporting can be adjusted.
PHP Fatal Errors
In the simplest terms Fatal Errors are errors that the PHP interpreter can simply not recover from. These type of errors will stop the script from continuing, meaning after these errors are encountered. The interpreter stops everything and displays the error.
The most common source of fatal errors is calling undeclared functions, or methods of objects, or classes (if using Object Oriented PHP), and mistakes in syntax.
Mistakes in syntax are typically followed by a message described as “Paser Error:….” with the approximate line number of the mistake. These errors are mostly always easy to find, and typically translate to missing semi-colon, extra/missing parenthesis or quote to name a few of the things.
Calling undeclared functions or classes means that the functions has not been declared yet, or that you possibly have a typo in the name of the function you are calling.
Correcting the typo — if applicable — will fix the issue.
If its a custom declared function or class, you must make sure that the file where the function is declared is being included. In this case, ensure that your include path is correct, and that the expected functions or class declarations are in the file.
Lastly fatal errors may also be related to a script taking too long to execute or using too much memory to execute — beyond the time or space allowed in php.ini. To fix this you would increase the time or space allotted or adjust the script to reduce the time or amount of memory the script takes to execute.
PHP Error Reporting
Error reporting setting can be adjusted in php.ini settings file that configures your php installation. Alternately PHP error reporting can be adjusted via the error_reporting() function.
It should be noted that disabling error reporting means that warnings, notices, and fatal errors may be muted when they occur, which may make debugging very difficult.
Below is a snippet of code from php.net that uses global constants to specify which types of errors should be displayed. Note that the constants are integer values.
// Turn off all error reporting
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE); //Bitwise OR
// 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); //Bitwise OR
// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE); //Bitwise xOR
// Report all PHP errors (as of 5.4.0)
// Report all PHP errors
// Same as error_reporting(E_ALL);
E_ERROR : 1
Generally everyone should try to write sound code that is free of errors. Writing error free code will ensure that your scripts work consistently across various PHP configurations and ensure that everyone — including yourself — can read and follow your code after it was initially written.
Ensure error reporting is on when you are debugging and that you know what the various errors means and how you can go about correcting them.