I tryed to email me debug_backtrace()
but it just prints out the array.
I need to assign it to a variable so that var_export can work with it.
How can i do that?

- 32,000
- 37
- 101
- 123
7 Answers
There is a function that "prints out the array", debug_print_backtrace
. You didn't mix those two up by any chance?
And both var_export() and print_r() by default print the result. You have to pass true
as the second parameter if you want them to return the result (so you can assign it to a variable)
$result = var_export(debug_backtrace(), true);

- 95,432
- 20
- 163
- 226
Send the following $msg :
$msg = print_r( debug_backtrace( ) , true ) ;
Not tested, but that's one way.

- 22,776
- 12
- 65
- 95
-
I think you've got an extra trailing parenthesis. – siliconrockstar Jul 26 '17 at 16:31
Debug Backtrace return an array so you can handle it like any array. For example :
$trace = debug_backtrace();
foreach ($trace as $i=>$t) {
$log .= $i .'=>'.$t['file'].' '.$t['line']."\n";
}
then you can email yourself the log line.

- 1,511
- 1
- 15
- 34
debug_backtrace
returns the array, but doesn't print it. There's probably something wrong with the code that handles the emailing of the report. Could you paste that piece of code here as well?

- 46
- 1
you can use print_r() to return the output to a variable and put it to a file
$backtrace = debug_backtrace(true);
$dump = print_r($backtrace, true);
$f=fopen('export.txt','a');
fwrite($f, date("Y-m-d H:i:s")." ".$dump."\n");
fclose($f);

- 11
- 1
I know this is old, but I found myself with the same problem. I can't confirm 100%, but it seems very likely that it relates to an out-of-memory error.
My scenario - on 3rd-party submission failure, send an email with debugging information (namely, where code failed). I too was saving $trace = debug_backtrace()
to a variable and then later using var_export($trace, true)
to embed it within an email. I'm using this helper function both inside the CodeIgniter framework and on a standalone page (legacy code).
The backtrace + export code worked fine in the standalone page, but printed (partial) results to the screen when in CodeIgniter. It actually only printed the first 2 traces before cutting off in the middle of a line.
When instead of echoing the whole backtrace, I only printed the line, file, fn, and args, everything worked fine.
My guess is that too much recursion from backtrace (marching through the CI objects) blew out memory, thus causing it to vomit the results to screen.

- 24,171
- 16
- 142
- 201