debug_backtrace
Description
The debug_backtrace of Error Handling for PHP generates a backtrace.
Syntax
debug_backtrace(
int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT,
int $limit = 0
): arrayParameters
options
A bitmask for the following options:
| Number | Constant | Description |
|---|---|---|
| 1 | DEBUG_BACKTRACE_PROVIDE_OBJECT | Whether or not to populate the "object" index. |
| 2 | DEBUG_BACKTRACE_IGNORE_ARGS | Whether or not to omit the "args" index, and thus all the function/method arguments, to save memory. |
limit
Limit the number of stack frames returned. By default (limit=0) it returns all stack frames.
Return
Returns an array of associative arrays. The possible returned elements are as follows:
| 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. If a method call, "->" is returned. If a static method call, "::" is returned. If a function call, nothing is returned. |
| args | array | If inside a function, this lists the functions arguments. If inside an included file, this lists the included file name(s). |
Examples
1 · void
<?
function myfunction1($myarg1, $myarg2)
{
$myarg1 += 1;
$myarg2 += 1;
myfunction2($myarg1, $myarg2);
}
function myfunction2($myarg1, $myarg2)
{
$myarg1 += 1;
$myarg2 += 1;
$return = debug_backtrace();
print_r($return);
}
myfunction1(0, 1);
Array
(
[0] => Array
(
[file] => /home/user/public_html/file.php
[line] => 8
[function] => myfunction2
[args] => Array
(
[0] => 2
[1] => 3
)
)
[1] => Array
(
[file] => /home/user/public_html/file.php
[line] => 20
[function] => myfunction1
[args] => Array
(
[0] => 1
[1] => 2
)
)
)2 · options · DEBUG_BACKTRACE_PROVIDE_OBJECT
<?
function myfunction1($myarg1, $myarg2)
{
$myarg1 += 1;
$myarg2 += 1;
myfunction2($myarg1, $myarg2);
}
function myfunction2($myarg1, $myarg2)
{
$myarg1 += 1;
$myarg2 += 1;
$options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
$return = debug_backtrace($options);
print_r($return);
}
myfunction1(0, 1);
Array
(
[0] => Array
(
[file] => /home/user/public_html/file.php
[line] => 8
[function] => myfunction2
[args] => Array
(
[0] => 2
[1] => 3
)
)
[1] => Array
(
[file] => /home/user/public_html/file.php
[line] => 22
[function] => myfunction1
[args] => Array
(
[0] => 1
[1] => 2
)
)
)3 · options · DEBUG_BACKTRACE_IGNORE_ARGS
<?
function myfunction1($myarg1, $myarg2)
{
$myarg1 += 1;
$myarg2 += 1;
myfunction2($myarg1, $myarg2);
}
function myfunction2($myarg1, $myarg2)
{
$myarg1 += 1;
$myarg2 += 1;
$options = DEBUG_BACKTRACE_IGNORE_ARGS;
$return = debug_backtrace($options);
print_r($return);
}
myfunction1(0, 1);
Array
(
[0] => Array
(
[file] => /home/user/public_html/file.php
[line] => 8
[function] => myfunction2
)
[1] => Array
(
[file] => /home/user/public_html/file.php
[line] => 22
[function] => myfunction1
)
)4 · limit
<?
function myfunction1($myarg1, $myarg2)
{
$myarg1 += 1;
$myarg2 += 1;
myfunction2($myarg1, $myarg2);
}
function myfunction2($myarg1, $myarg2)
{
$myarg1 += 1;
$myarg2 += 1;
$options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
$limit = 1;
$return = debug_backtrace($options, $limit);
print_r($return);
}
myfunction1(0, 1);
Array
(
[0] => Array
(
[file] => /home/user/public_html/file.php
[line] => 8
[function] => myfunction2
[args] => Array
(
[0] => 2
[1] => 3
)
)
)