Error and Logging Functions in PHP: PART 1

Introduction

An error is an expected or unexpected event that occurs when a PHP application runs. Every well-constructed PHP application should have error handling. An "expected" error can be a database query. An "unexpected" error is one that assumes a particular application state which, for some as yet unknown reason does not exist. So the PHP error and logging function allows error handling and logging. So I explain some of them in this article
.

PHP debug_backtrace() function


The PHP debug_backtrace function generates a backtrace and it returns an array of associative arrays. The possible returned elements are:

Name Type Description
function string The current function name.
line integer The current line number.
file string The current file name.
class string The current class name.
object object The current object.
type string The current call type and possible calls are:
  • Returns: "->" - Method call
  • Returns: "::" - Static method call
  • Returns nothing - Function call
args array If inside a function, this lists the functions arguments. If inside an included file, this lists the included file name.

Syntax

debug_backtrace()

Example

An example of the
function is:

<?php

function one($str1, $str2)

  {

  two("bar", "wom");

  }

function two($str1, $str2)

  {

  three("baz", "wom");

  }

function three($str1, $str2)

  {

  print_r(debug_backtrace());

  }

echo "<pre>";one

one("foo", "bar");

?>

As you can see, the script calls function one(), that calls two(), that calls three(). The debug_backtrace() will return an array of the steps.

Output


debug-trace-function-in-php.jpg

PHP debug_print_backtrace() function

The PHP debug_print_backtrace function prints a backtrace.

Syntax

debug_ptint_backtrace()

Example

An example of the
function is:

<?
php
function
foo($arg1, $arg2, $arg3)
{
   
bar();
}

function
bar()
{

echo
"<pre>";
   
print_r(debug_backtrace());
}

foo
('one', 'two', 'three');
?>


Output

dubag-print-trace-function-in-php.jpg 

PHP error_get_last() function

The PHP error_get_last function gets the last error that occurred and it returns an associative array describing the last error with the key:

  • "type" means error type.
  • "message" means error message.
  • "file" means the file where the error occurred.
  • "line" means the line where the error occurred.

Syntax

error_get_last()

Example

An example of the
function is:

<?
php
echo
$var;
echo
"<pre>";
print_r
(error_get_last());
?>


Output

error-get-last-function-in-php.jpg 

PHP error_reporting() function

The PHP error_reporting function specifies which errors are to be reported and it returns the old error_reporting level or the current level if no level parameter is given.

Syntax

error_reporting(repoet_level)

Parameter in error_reporting function

The parameter of the function is:

Parameter Description
report_level It specifies the error report level for the current script.

Example

An example of the function is:

<?
php
error_reporting
(E_ALL);
echo
error_reporting()."</br>";
$
old_error_reporting = error_reporting(error_reporting() ^ E_NOTICE);
echo
error_reporting()."</br>";
error_reporting
($old_error_reporting);
echo
error_reporting();
?>


Output

 error-reporting-function-in-php.jpg


Similar Articles