PHP debug_print_backtrace - Syntax
debug_print_backtrace(int $options = 0, int $limit = 0): void
debug_print_backtrace() writes the current call stack to the output. Use debug_backtrace() when you need an array of frames instead.
debug_print_backtrace()
debug_print_backtrace(int $options = 0, int $limit = 0): void
| $options | Use DEBUG_BACKTRACE_IGNORE_ARGS to omit argument values. The default is 0. |
|---|---|
| $limit | Maximum frame count. Zero includes the whole stack. |
DEBUG_BACKTRACE_IGNORE_ARGS omits argument values from stack frames.
A teacher wants to see how the welcome page reached its greeting helper. Inspect the stack at greetings() and follow the calling frames outward.
<?php
class HelloWorld {
public function greetings() {
echo("Hello World \n");
// Back Tracing Operation
// Default options and no frame limit
debug_print_backtrace();
}
}
$obj = new HelloWorld();
$obj->greetings();
Hello World #0 HelloWorld->greetings() called at [~\Documents\debug-print-backtrace.php:14]
A nested message helper produces unexpected output on a class page. Start inside printMessage() and follow the callers that reached that method.
<?php
class HelloWorld {
public function greetings($person) {
$message = "Hello " . $person;
// Calling a private method to print the message
$this->printMessage($message);
}
private function printMessage($message) {
echo($message . "\n");
// Back Tracing Operation
// Default options and no frame limit
debug_print_backtrace();
}
}
$obj = new HelloWorld();
$obj->greetings("John");
Hello John #0 HelloWorld->printMessage(Hello John) called at [~\Documents\debug-print-backtrace.php:8] #1 HelloWorld->greetings(John) called at [~\Documents\debug-print-backtrace.php:21]
A support engineer needs to identify the calling functions for a failed classroom request. Omit argument values when the frame names alone answer the question.
<?php
class HelloWorld {
public function greetings($person) {
$message = "Hello " . $person;
// Calling a private method to print the message
$this->printMessage($message);
}
private function printMessage($message) {
echo($message . "\n");
// Back Tracing Operation
// with DEBUG_BACKTRACE_IGNORE_ARGS and limit 0
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
}
}
$obj = new HelloWorld();
$obj->greetings("John");
Hello John #0 HelloWorld->printMessage() called at [~\Documents\debug-print-backtrace.php:8] #1 HelloWorld->greetings() called at [~\Documents\debug-print-backtrace.php:21]
A busy help desk needs a compact trace for a particular request. Limit it to one frame and omit arguments to show just the immediate call site.
<?php
class HelloWorld {
public function greetings($person) {
$message = "Hello " . $person;
// Calling a private method to print the message
$this->printMessage($message);
}
private function printMessage($message) {
echo($message . "\n");
// Back Tracing Operation
// with DEBUG_BACKTRACE_IGNORE_ARGS and limit 1
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
}
}
$obj = new HelloWorld();
$obj->greetings("John");
Hello John #0 HelloWorld->printMessage() called at [~\Documents\debug-print-backtrace.php:8]
Reference: PHP manual — debug_print_backtrace()