php debug backtrace

Solutions on MaxInterview for php debug backtrace by the best coders in the world

showing results for - "php debug backtrace"
Francesco
21 Nov 2017
1Giving a basic example...
2
3<?php
4error_reporting(E_ALL);
5ini_set('display_errors', 1);
6
7echo "Start...";
8print_r(debug_backtrace());
9
10function t1 ()  {
11    echo "Start t1...";
12    print_r(debug_backtrace());
13
14}
15
16function t2()   {
17    echo "Start t2...";
18    print_r(debug_backtrace());
19    t1();
20}
21
22echo "before calls...";
23print_r(debug_backtrace());
24t1();
25t2();
26Will output...
27
28Start...Array
29(
30)
31before calls...Array
32(
33)
34Start t1...Array
35(
36    [0] => Array
37        (
38            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
39            [line] => 22
40            [function] => t1
41            [args] => Array
42                (
43                )
44
45        )
46
47)
48Start t2...Array
49(
50    [0] => Array
51        (
52            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
53            [line] => 23
54            [function] => t2
55            [args] => Array
56                (
57                )
58
59        )
60
61)
62Start t1...Array
63(
64    [0] => Array
65        (
66            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
67            [line] => 17
68            [function] => t1
69            [args] => Array
70                (
71                )
72
73        )
74
75    [1] => Array
76        (
77            [file] => /home/nigel/workspace/PHPTest/TestSource/part3.php
78            [line] => 23
79            [function] => t2
80            [args] => Array
81                (
82                )
83
84        )
85
86)
87I hope this shows that all the debug_backtrace function does is return what has been called so far. So when you first call t1, it is simply a stack trace of the call to t1. The same for the start of t2, but when t2 calls t1, the trace lists the call to t2 and t1.
88
89But also as you can see, that debug_backtrace from Start.. shows nothing, as there are no levels of code that have caused this trace to be printed.
90
91In your case it calls debug_backtrace and uses the or die() to say 'if debug_backtrace returns nothing, then die()'