Richard C.
—When an error occurs in PHP and you want to understand the flow of program logic that occurred, it’s useful to be able to print the call stack — all the functions that were called up to this point.
Use debug_print_backtrace();
.
Below is a simple example PHP script with three functions called in sequence: A
, B
, and C
. Function C
prints the call stack:
<?php A("a"); function A($a) { B(); } function B() { C(); } function C() { debug_print_backtrace(); } ?>
Put this script in a file called test.php
and run it with php test.php
. The output is:
#0 /home/user/test.php(10): C() #1 /home/user/test.php(5): B() #2 /home/user/test.php(19): A('a')
This is the fastest way to print the stack trace.
The A('a')
indicates the function A
received parameters. You probably don’t need to see this, so to make the output neater use: debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
.
To print the full details of every call, including parameter values, use:
var_dump(debug_backtrace());
The output will be:
array(3) { [0]=> array(4) { ["file"]=> string(35) "/home/user/test.php" ["line"]=> int(9) ["function"]=> string(1) "C" ["args"]=> array(0) { } } [1]=> array(4) { ["file"]=> string(35) "/home/user/test.php" ["line"]=> int(5) ["function"]=> string(1) "B" ["args"]=> array(0) { } } [2]=> array(4) { ["file"]=> string(35) "/home/user/test.php" ["line"]=> int(2) ["function"]=> string(1) "A" ["args"]=> array(1) { [0]=> string(1) "a" } } }
To ignore function arguments, you can use var_dump(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
.
Some users on StackOverflow and the PHP manual website say that backtrace
can cause memory problems with very large applications. If you encounter this, you could instead try printing the call stack with:
$e = new Exception(); var_dump($e->getTraceAsString());
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.