Print PHP Call Stack

Given a PHP code and task is to print call stack for this PHP code. In the given PHP code, a child_func() function calls parent_func() function which further calls grandparent_func() function resulting in a call stack.

Approach 1: Print the call stack using debug_print_backtrace() function.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print PHP call stack
  
// Function to call parent_func 
function child_func() {
    parent_func();
}
  
// Function to call grandparent_func
function parent_func() {
    grandparent_func();
}
  
// Function to print call stack
function grandparent_func() {
    debug_print_backtrace();
}
  
// Main function call
child_func();
  
?>

chevron_right


Output:

#0  grandparent_func() called at [/home/905a3b4d90f10b30521fedcb56c99fba.php:12]
#1  parent_func() called at [/home/905a3b4d90f10b30521fedcb56c99fba.php:7]
#2  child_func() called at [/home/905a3b4d90f10b30521fedcb56c99fba.php:21]

Approach 2: Print the call stack using debug_backtrace() function.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print PHP call stack
  
// Function to call parent_func 
function child_func() {
    parent_func();
}
  
// Function to call grandparent_func
function parent_func() {
    grandparent_func();
}
  
// Function to print call stack
function grandparent_func() {
    var_dump(debug_backtrace());
}
  
// Main function call
child_func();
  
?>

chevron_right


Output:

array(3) {
  [0]=>
  array(4) {
    ["file"]=>
    string(42) "/home/2b81f040639170c49a6a58adb23d5154.php"
    ["line"]=>
    int(12)
    ["function"]=>
    string(16) "grandparent_func"
    ["args"]=>
    array(0) {
    }
  }
  [1]=>
  array(4) {
    ["file"]=>
    string(42) "/home/2b81f040639170c49a6a58adb23d5154.php"
    ["line"]=>
    int(7)
    ["function"]=>
    string(11) "parent_func"
    ["args"]=>
    array(0) {
    }
  }
  [2]=>
  array(4) {
    ["file"]=>
    string(42) "/home/2b81f040639170c49a6a58adb23d5154.php"
    ["line"]=>
    int(21)
    ["function"]=>
    string(10) "child_func"
    ["args"]=>
    array(0) {
    }
  }
}

Approach 3: The getTraceAsString() member function of Exception class returns a call stack.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print PHP call stack
  
// Function to call parent_func 
function child_func() {
    parent_func();
}
  
// Function to call grandparent_func
function parent_func() {
    grandparent_func();
}
  
// Function to print call stack
function grandparent_func() {
    $e = new Exception;
    var_dump($e->getTraceAsString());
}
  
// Main function call
child_func();
  
?>

chevron_right


Output:

string(207) "#0 /home/8d8303d43667a4915d43dab7d63de26d.php(12): grandparent_func()
#1 /home/8d8303d43667a4915d43dab7d63de26d.php(7): parent_func()
#2 /home/8d8303d43667a4915d43dab7d63de26d.php(22): child_func()
#3 {main}"


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.