Tag Archives: Error

Error Display

• By default, all errors included in error_reporting are reported in php´s output

• This behavior can – and should – be deactivated on production systems using the display_errors = off php.ini setting

• Errors should still be logged, using the log_errors = on php.ini setting

• The error log is a file set in the error_log php configuration setting. if set to syslog, errors are logged in the system log independent on the operating system used.

Error Levels

• PHP supports several types of errors

o notices at runtime
o errors during parsing (prevents code execution)
o warnings at runtime
o fatal errors at runtime (stop code execution)
o Core errors and warnings
o user-defined notices, warnings, and errors

• php configuration setting error_reporting, or php’s error_reporting() function may be used to define which kinds of errors shall be reported

• Value is an integer, or – much more convenient – a bitmask based on pre-defined constants

o E_NOTICE, E_PARSE, E_WARNING, E_ERROR

o E_CORE_WARNING, E_CORE_ERROR

o E_USER_NOTICE, E_USER_WARNING ,E_USER_ERROR

o E_STRICT(„best practices“ notices)

o E_DEPRECATED (features that might disappear in future php versions)

o E_ALL(everything) o and some more

• Typical production setting: E_ALL & ~E_DEPRECATED & ~E_STRICT

Error Class

• Since php 7, many errors php reports now throw an error exception, not a fatal error as before.

• Subclasses of error exist for the specific type of error, such as parseerror or typeerror.

• To facilitate backwards compatibility, error is not derived from exception.

• Both error and exception implement the throwable interface

set_exception_handler

It sets a user-defined function to handle exception

Example

<?php
function customException($exception){
echo  $exception->getMessage();
}
set_exception_handler(‘customException’);
throw new Exception(‘Uncaught Exception occurred’);
?>

Output

Exception: Uncaught Exception occurred

set_error_handler

It sets a user-defined function to handle errors

Example

<?php
function errorFunc($errno, $errstr, $errfile, $errline) {
echo $errstr.” on ” . $errfile .” at “. $errline;
}
set_error_handler(“errorFunc”);
$val=5;
if ($test>1)  trigger_error(“An error occured”);
?>

Output

Undefined variable: test on /var/www/test/index.php at 7

restore_exception_handler

It r restores the previous exception handler

Example

<?php

function customError($errno, $errstr, $errfile, $errline)  {
echo $errno.””.$errstr.” , File name – “.$errfile.” at line No”.$errline;
}

set_error_handler(“customError”);

$val=5;

if ($val>1)  trigger_error(“An error occured”);

restore_error_handler();

if ($val>5)  trigger_error(“An error occured”);

?>

Output

exception_handler_1 – This triggers the first exception handler…

restore_error_handler

It restores the previous error handler

Example

<?php

function customError($errno, $errstr, $errfile, $errline)  {
echo $errno.””.$errstr.” , File name – “.$errfile.” at line No”.$errline;
}

set_error_handler(“customError”);

$val=5;

if ($val>1)  trigger_error(“An error occured”);

restore_error_handler();

if ($val>5)  trigger_error(“An error occured”);

?>

Output

1024An error occured , File name – /var/www/test/date/date.php at line No11

error_reporting

It specifies which errors are occurred.

Example

<?php
error_reporting(E_ALL);
?>

E_ERROR ,E_WARNING,E_PARSE,E_NOTICE,E_CORE_ERROR,E_CORE_WARNING ,E_COMPILE_ERROR,E_COMPILE_WARNING,E_USER_ERROR,E_USER_WARNING,E_USER_NOTICE,E_STRICT ,E_RECOVERABLE_ERROR,E_ALL

Output

It shows all the errors occurred including waring,notice etc