查詢

debug_print_backtrace()函式—用法及示例

「 列印當前的呼叫棧資訊,主要用於除錯和錯誤排查 」


函式名稱:debug_print_backtrace()

函式描述:debug_print_backtrace() 函式用於列印當前的呼叫棧資訊,主要用於除錯和錯誤排查。

適用版本:這個函式是在 PHP 4 起可用,並且在 PHP 7 中仍然有效。

語法:debug_print_backtrace(bool $return = false) : void

引數:

  • $return(可選):設定為 true 時,函式將返回撥用堆疊資訊的字串,預設為 false。如果設定為 true,則不會直接輸出呼叫堆疊資訊而是返回字串。

返回值:

  • 如果 $return 引數為 true,則返回撥用堆疊資訊的字串。
  • 如果 $return 引數為 false(預設),則函式沒有返回值。

示例:

  1. 基本用法:
function foo() {
    debug_print_backtrace();
}

function bar() {
    foo();
}

bar();

輸出:

#0 foo() called at /path/to/file.php:4
#1 bar() called at /path/to/file.php:8
  1. 使用 $return 引數:
function foo() {
    return debug_print_backtrace(true);
}

function bar() {
    return foo();
}

$trace = bar();
echo $trace;

輸出:

#0 foo() called at /path/to/file.php:4
#1 bar() called at /path/to/file.php:8
補充糾錯
上一個函式: dcngettext()()函式
下一個函式: debug_zval_dump()函式
熱門PHP函式
分享連結