PHP debug_backtrace() Function
The PHP debug_backtrace() function is used to generates a PHP backtrace.
Syntax
debug_backtrace(options, limit)
Parameters
options |
Optional. Specify this parameter as a bitmask for the following options:
|
limit |
Optional. This parameter can be used to limit the number of stack frames returned. By default (limit=0) it returns all stack frames. |
Return Value
Returns an array of associative arrays. The possible returned elements are as follows:
Name | Type | Description |
---|---|---|
function | string | The current function name. |
line | int | 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. Possible calls:
|
args | array | If inside a function, it lists the functions arguments. If inside an included file, it lists the included file names. |
Example: debug_backtrace() example
The example below shows the usage of debug_backtrace() function.
<?php function test_a($x, $y){ echo "\$x + \$y = ".($x + $y)."\n"; var_dump(debug_backtrace()); } test_a(10, 20); ?>
The output of the above code will be:
$x + $y = 30 array(1) { [0]=> array(4) { ["file"]=> string(39) "Main.php" ["line"]=> int(7) ["function"]=> string(6) "test_a" ["args"]=> array(2) { [0]=> int(10) [1]=> int(20) } } }
Example: debug_backtrace() example
Consider one more example on debug_backtrace() function.
<?php function test_a($str){ test_b($str); } function test_b($str){ echo "Hi $str \n"; var_dump(debug_backtrace()); } test_a('friend'); ?>
The output of the above code will be:
Hi friend array(2) { [0]=> array(4) { ["file"]=> string(39) "Main.php" ["line"]=> int(3) ["function"]=> string(6) "test_b" ["args"]=> array(1) { [0]=> string(6) "friend" } } [1]=> array(4) { ["file"]=> string(39) "Main.php" ["line"]=> int(11) ["function"]=> string(6) "test_a" ["args"]=> array(1) { [0]=> string(6) "friend" } } }
❮ PHP Error Handling Reference