debug_backtrace 함수

debug_backtrace 함수는 현재 호출 스택에 대한 정보를 포함하는 배열을 반환합니다. 여기에는 호출된 함수, 해당 함수가 위치한 파일, 호출이 발생한 줄 번호에 대한 데이터가 포함됩니다. 첫 번째 매개변수는 출력을 제한할지 여부를 결정하고, 두 번째 매개변수는 스택의 몇 단계를 건너뛸 것인지를 지정합니다.

구문

phpdebug_backtrace([options], [limit]);

예제

호출 스택을 출력하기 위한 함수 사용의 간단한 예:

php<?php
  function test() {
    var_dump(debug_backtrace());
  }
  test();
?>

코드 실행 결과:

phparray(1) {
  [0]=>
  array(4) {
  ["file"]=>
  string(17) "/path/to/file.php"
  ["line"]=>
  int(5)
  ["function"]=>
  string(4) "test"
  ["args"]=>
  array(0) {
  }
  }
}

예제

출력 제한을 위한 매개변수 사용:

php<?php
  function inner() {
    var_dump(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1));
  }
  function outer() {
    inner();
  }
  outer();
?>

코드 실행 결과:

phparray(1) {
  [0]=>
  array(3) {
  ["file"]=>
  string(17) "/path/to/file.php"
  ["line"]=>
  int(7)
  ["function"]=>
  string(5) "inner"
  }
}

함께 보기

  • 변수 정보를 출력하는 함수 var_dump
  • 오류 메시지를 전송하는 함수 error_log