PHP Debug efficiently with debug_backtrace

Hello today I write a small function for debug your PHP code. Its simple but powerful –

$bkTrace=function ($stack) {
 $output = '';
 
 $stackLen = count($stack);
 for ($i = 1; $i < $stackLen; $i++) {
 $entry = $stack[$i];
 
 $func = (array_key_exists('class', $entry)?$entry['class'].'\\':'').$entry['function'] . '(';
 $argsLen = count($entry['args']);
 for ($j = 0; $j < $argsLen; $j++) {
 $my_entry = $entry['args'][$j];
 if (is_string($my_entry)) {
 $func .= $my_entry;
 }
 if ($j < $argsLen - 1) $func .= ', ';
 }
 $func .= ')';
 
 $entry_file = 'NO_FILE';
 if (array_key_exists('file', $entry)) {
 $entry_file = $entry['file'];
 }
 $entry_line = 'NO_LINE';
 if (array_key_exists('line', $entry)) {
 $entry_line = $entry['line'];
 }
 $output .= $entry_file . ':' . $entry_line . ' - ' . $func . PHP_EOL;
 }
 return $output;
 };
 echo '<pre>'.$bkTrace(debug_backtrace()); exit();

 

Also for response with warning you can use above method like below –

trigger_error(‘warning @’.$bkTrace(debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2)), E_USER_WARNING);

 

nJoy debugging…


			
Posted in php, webdevelopmentTagged , ,

2 thoughts on “PHP Debug efficiently with debug_backtrace

Comments are closed.