Take the 2-minute tour ×

I'm looking for a way to print the call stack in PHP.

Bonus points if the function flushes the IO buffer.

share|improve this question
possible duplicate of How can I get PHP to produce a backtrace upon errors? –  Gordon Nov 17 '11 at 9:22
...but these responses are better. –  Steve May 7 '12 at 6:40

10 Answers 10

up vote 73 down vote accepted

If you want to generate a backtrace, you are looking for debug_backtrace and/or debug_print_backtrace.

The first one will, for instance, get you an array like this one (quoting the manual) :

array(2) {
array(4) {
    ["file"] => string(10) "/tmp/a.php"
    ["line"] => int(10)
    ["function"] => string(6) "a_test"
    array(1) {
      [0] => &string(6) "friend"
array(4) {
    ["file"] => string(10) "/tmp/b.php"
    ["line"] => int(2)
    ["args"] =>
    array(1) {
      [0] => string(10) "/tmp/a.php"
    ["function"] => string(12) "include_once"

They will apparently not flush the I/O buffer, but you can do that yourself, with flush and/or ob_flush.

(see the manual page of the first one to find out why the "and/or" ;-) )

share|improve this answer

If you want a stack trace which looks very similar to how php formats the exception stack trace than use this function I wrote:

function debug_backtrace_string() {
    $stack = '';
    $i = 1;
    $trace = debug_backtrace();
    unset($trace[0]); //Remove call to this function from stack trace
    foreach($trace as $node) {
        $stack .= "#$i ".$node['file'] ."(" .$node['line']."): "; 
        if(isset($node['class'])) {
            $stack .= $node['class'] . "->"; 
        $stack .= $node['function'] . "()" . PHP_EOL;
    return $stack;

This will return a stack trace formatted like this:

#1 C:\Inetpub\sitename.com\modules\sponsors\class.php(306): filePathCombine()
#2 C:\Inetpub\sitename.com\modules\sponsors\class.php(294): Process->_deleteImageFile()
#3 C:\Inetpub\sitename.com\VPanel\modules\sponsors\class.php(70): Process->_deleteImage()
#4 C:\Inetpub\sitename.com\modules\sponsors\process.php(24): Process->_delete() 
share|improve this answer

Backtrace dumps a whole lot of garbage that you don't need. It takes is very long, difficult to read. All you usuall ever want is "what called what from where?" Here is a simple static function solution. I usually put it in a class called 'debug', which contains all of my debugging utility functions.

class debugUtils {
    public static function callStack($stacktrace) {
        print str_repeat("=", 50) ."\n";
        $i = 1;
        foreach($stacktrace as $node) {
            print "$i. ".basename($node['file']) .":" .$node['function'] ."(" .$node['line'].")\n";

You call it like this:


And it produces output like this:

 1. DatabaseDriver.php::getSequenceTable(169)
 2. ClassMetadataFactory.php::loadMetadataForClass(284)
 3. ClassMetadataFactory.php::loadMetadata(177)
 4. ClassMetadataFactory.php::getMetadataFor(124)
 5. Import.php::getAllMetadata(188)
 6. Command.php::execute(187)
 7. Application.php::run(194)
 8. Application.php::doRun(118)
 9. doctrine.php::run(99)
 10. doctrine::include(4)
share|improve this answer
So make it $element, or rename $element to $node, big deal... –  Nikos Ventouras Aug 8 '12 at 9:35

To log the trace

$e = new Exception;
error_log(var_export($e->getTraceAsString(), true));

Thanks @Tobiasz

share|improve this answer

More readable than debug_backtrace():

$e = new Exception;

#2 /usr/share/php/PHPUnit/Framework/TestCase.php(626): SeriesHelperTest->setUp()
#3 /usr/share/php/PHPUnit/Framework/TestResult.php(666): PHPUnit_Framework_TestCase->runBare()
#4 /usr/share/php/PHPUnit/Framework/TestCase.php(576): PHPUnit_Framework_TestResult->run(Object(SeriesHelperTest))
#5 /usr/share/php/PHPUnit/Framework/TestSuite.php(757): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#6 /usr/share/php/PHPUnit/Framework/TestSuite.php(733): PHPUnit_Framework_TestSuite->runTest(Object(SeriesHelperTest), Object(PHPUnit_Framework_TestResult))
#7 /usr/share/php/PHPUnit/TextUI/TestRunner.php(305): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult), false, Array, Array, false)
#8 /usr/share/php/PHPUnit/TextUI/Command.php(188): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#9 /usr/share/php/PHPUnit/TextUI/Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#10 /usr/bin/phpunit(53): PHPUnit_TextUI_Command::main()
#11 {main}"
share|improve this answer
Ahhhhh. Thanks. –  shaune Mar 10 '12 at 0:16
Damn, this is so much better, why couldn't they make this the default output for debug_print_backtrace()? Could have added a boolean parameter "returnTrace" for those who want it in a variable, not echoed, and it would have been perfect! –  jurchiks May 21 '13 at 8:28
I dont know how many months i been trying to figure out how to do that never thought it would work –  WojonsTech Oct 22 '13 at 18:54
This solution also appears to take less memory than capturing the output of debug_backtrace() as an array and then printing it using print_r(), which is what I had been doing until I saw this! –  Peter Nov 13 '13 at 12:38
great solution! –  Asped May 28 at 10:48

Does that do what you want?

share|improve this answer

Use debug_backtrace to get a backtrace of what functions and methods had been called and what files had been included that led to the point where debug_backtrace has been called.

share|improve this answer

You might want to look into debug_backtrace, or perhaps debug_print_backtrace.

share|improve this answer


share|improve this answer

See debug_print_backtrace. I guess you can call flush afterwards if you want.

share|improve this answer

Your Answer


