Troubleshooting in PHP

Turned on by Default

error_reporting = E_ALL display_errors = On

Turned of by Default

error_log = syslog log_errors = On html_errors = On

Using PHP Functions for Debugging

error_reporting

error_reporting is built in function in php. if you don’t have access to php.ini file you can use this funtion to do it.

// turn off error reporting in any file
error_reporting(0);

// turn off error reporting in any file
error_reporting(E_ALL);

$a = 2;
if (($a < 2)){
    echo "hello world";
}

set_cookie('error', true);

other use full functions

var_dump($a);

Types of Errors

Syntax/Parse Errors

These error are because of typos or misused charachters in code. Comiple time error.

Warnings

warning will not terminate your code but will warn you about the problems you may come acros in future. For example missing include files. the program wil give the warning about include files but will continue to run.

Fatal Errors

Fatal errors will terminate the programe. php call these issues unrecoverable. For example calling a function without defining it.

Notice Errors

nothing breaks. example: use of undefined variables.

Logical Errors

for example infinite loops

Blank page or 500 error

this will happen if you have turned off error reporting in php. this could due to syntax error.

Most of the time php error message shows the line number on which error occured. but the actual line where it has occured is before that line.

  • look for semicolons. when you see the word unexpected you could be missing semicolon.
  • T_CONSTANT_ENCAPSED_STRING means une xpected white space.
  • failed to open stream this happens to include statement to a file that does not exist.
  • warning: cannot modify header information - headers already sent
  • Notic: undefined offset. undefined constant or index out of the range.

Exceptions

// must return positive integers
function multiply($a, $b){
   $x = $a * $b;
   if ($x < 0){
       throw new Exception("The result is not positive.");
   } else {
       return $x;
   }
}

try {
    echo multiply(-5, 4);
} catch(Exception $e) {
    echo $e->getMessage();
}

Previous