Last updated November 8, 2011. Created by beginner on October 17, 2005.
Edited by jhodgdon, Andrew Schulman, mfb, willmoy. Log in to edit this page.
Adjusting the error reporting level
Drupal 6.x releases ignore E_NOTICE, E_STRICT, and E_DEPRECATED notices for the benefit of production sites. To view all PHP errors on development or testing sites, you may change includes/common.inc from:
<?php
if ($errno & (E_ALL ^ E_DEPRECATED ^ E_NOTICE)) {
?>to:
<?php
if ($errno & (E_ALL | E_STRICT)) {
?>Drupal 7.x releases report any error levels which are part of E_ALL, and allow PHP to be configured to report additional error levels, such as E_STRICT. To view all PHP errors on development or testing sites, you may set
php_value error_reporting -1.htaccess file.
Use of isset() or !empty()
If you want to test the value of a variable, array element or object property, you may need to use
<?php
if (isset($var))
?><?php
if (!empty($var))
?><?php
if ($var)
?>$var has not been defined.
The difference between isset() and !empty() is that unlike !empty(), isset() will return TRUE even if the variable is set to an empty string or zero. In order to decide which one to use, consider whether '' or 0 are valid and expected values for your variable.
The following code may trigger an E_NOTICE error:
<?php
function _form_builder($form, $parents = array(), $multiple = FALSE) {
// (...)
if ($form['#input']) {
// some code (...)
}
}
?>Here, the variable $form is passed on to the function. If $form['#input'] evaluates as true, some code is executed. However, if $form['#input'] does not exist, the function outputs the following error message: notice: Undefined index: #input in includes/form.inc on line 194.
Even though the array $form is already declared and passed to the function, each array index must be explicitly declared. The previous code should read:
<?php
function _form_builder($form, $parents = array(), $multiple = FALSE) {
// (...)
if (!empty($form['#input'])) {
// some code (...)
}
}
?>Beware!
The function isset() returns TRUE when the variable is set to 0, but FALSE if the variable is set to NULL. In some cases, is_null() is a better choice, especially when testing the value of a variable returned by an SQL query.
Comments
Devel's Backtrace Error Handler
Install the Devel module and enable its backtrace error handler. This will not only activate all error types (no need to make the changes explained above), it will also show you the full call stack at the point where the error occurs.
Many notices can appear not only in your own code but even in core code, for example if you forget to set a certain mandatory key in an array that you pass to a core function. The call stack will let you find out quickly where the real problem is.
Also, it is quite common that there are notices and warnings leading up to crashes, but they are lost in the crash. The backtrace error handler can show these to you, and fixing the first notice may also fix the crash, because quite often that's where things start to go wrong.