In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "PHP how to print tracking debugging information". In daily operation, I believe many people have doubts about how PHP prints tracking debugging information. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how PHP prints tracking debugging information". Next, please follow the editor to study!
PHP prints trace debugging information
For most compiled languages, such as C, Java, and C #, we can easily debug breakpoints, but PHP must install XDebug and do complex configuration in the editor to achieve breakpoint debugging. However, if you simply debug and look at stack backtracking, PHP has already prepared two functions for us to easily see how the program is called at run time.
Debug_backtrace ()
As you can see from the literal meaning of this method, it means debugging backtracking, and what is returned is an array of backtracking information.
Function a_test ($str)
{
Echo "Hi: $str", PHP_EOL
Var_dump (debug_backtrace ())
}
Var_dump (debug_backtrace ())
A_test ("A")
/ / Hi: A/Users/zhangyue/MyDoc/ blog post / dev-blog/php/202004/source/PHP print tracking debugging information. Php: 7:
/ / array (1) {
/ / [0] = >
/ / array (4) {
/ / 'file' = >
/ / string (93) "/ Users/zhangyue/MyDoc/ blog article / dev-blog/php/202004/source/PHP print tracking debugging information .php"
/ / 'line' = >
/ / int (12)
/ / 'function' = >
/ / string (6) "a_test"
/ / 'args' = >
/ / array (1) {
/ / [0] = >
/ / string (1) "A"
/ /}
/ /}
/ /}
This method must be called in the function, and there is no content to use outside the function method. In terms of content, it outputs information about the function, such as _ _ FILE__, _ _ LINE__, _ _ FUNCTION__, $argv, and so on. In fact, it is about the function in which the current line is printed.
Of course, we can also nest a few more layers of functions to see what the printed content is.
Function b_test () {
C_test ()
}
Function c_test () {
A_test ("b-> c-> a")
}
B_test ()
/ / Hi: B-> c-> a
/ / Users/zhangyue/MyDoc/ blog post / dev-blog/php/202004/source/PHP print tracking debugging information. Php: 7:
/ / array (3) {
/ / [0] = >
/ / array (4) {
/ / 'file' = >
/ / string (93) "/ Users/zhangyue/MyDoc/ blog article / dev-blog/php/202004/source/PHP print tracking debugging information .php"
/ / 'line' = >
/ / int (37)
/ / 'function' = >
/ / string (6) "a_test"
/ / 'args' = >
/ / array (1) {
/ / [0] = >
/ / string (11) "b-> c-> a"
/ /}
/ /}
/ / [1] = >
/ / array (4) {
/ / 'file' = >
/ / string (93) "/ Users/zhangyue/MyDoc/ blog article / dev-blog/php/202004/source/PHP print tracking debugging information .php"
/ / 'line' = >
/ / int (33)
/ / 'function' = >
/ / string (6) "c_test"
/ / 'args' = >
/ / array (0) {
/ /}
/ /}
/ / [2] = >
/ / array (4) {
/ / 'file' = >
/ / string (93) "/ Users/zhangyue/MyDoc/ blog article / dev-blog/php/202004/source/PHP print tracking debugging information .php"
/ / 'line' = >
/ / int (40)
/ / 'function' = >
/ / string (6) "b_test"
/ / 'args' = >
/ / array (0) {
/ /}
/ /}
/ /}
Yes, the output order of the array is the execution order of a stack, and b_test () is called first, so it is at the bottom of the stack, and the corresponding output is the last element in the array.
A similar method is used in a class.
Class A {
Function test_a () {
$this- > test_b ()
}
Function test_b () {
Var_dump (debug_backtrace ())
}
}
$a = new A ()
$a-> test_a ()
/ / Users/zhangyue/MyDoc/ blog post / dev-blog/php/202004/source/PHP print tracking debugging information. Php: 90:
/ / array (2) {
/ / [0] = >
/ / array (7) {
/ / 'file' = >
/ / string (93) "/ Users/zhangyue/MyDoc/ blog article / dev-blog/php/202004/source/PHP print tracking debugging information .php"
/ / 'line' = >
/ / int (87)
/ / 'function' = >
/ / string (6) "test_b"
/ / 'class' = >
/ / string (1) "A"
/ / 'object' = >
/ / class Atoll 1 (0) {
/ /}
/ / 'type' = >
/ / string (2) "- >"
/ / 'args' = >
/ / array (0) {
/ /}
/ /}
/ / [1] = >
/ / array (7) {
/ / 'file' = >
/ / string (93) "/ Users/zhangyue/MyDoc/ blog article / dev-blog/php/202004/source/PHP print tracking debugging information .php"
/ / 'line' = >
/ / int (95)
/ / 'function' = >
/ / string (6) "test_a"
/ / 'class' = >
/ / string (1) "A"
/ / 'object' = >
/ / class Atoll 1 (0) {
/ /}
/ / 'type' = >
/ / string (2) "- >"
/ / 'args' = >
/ / array (0) {
/ /}
/ /}
/ /}
When used in a class, there is an extra object field in the array item that displays information about the class in which the method belongs.
The function declaration of debug_backtrace () is:
Debug_backtrace ([int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT [, int $limit = 0]]): array
Where\ $options has two constants that can be defined, DEBUG_BACKTRACE_PROVIDE_OBJECT indicates whether to populate the index of "object" and whether DEBUG_BACKTRACE_IGNORE_ARGS ignores the index of "args", including all the parameters of function/method, which can save memory overhead. $limits can be used to limit the number of stack frames returned, and the default is 0 to return all stacks.
Debug_backtrace () and the debug_print_backtrace () method to be described below both support the require/include file and the code in eval (). When embedding a file, it will output the path to the embedded file, which you can try on your own.
Debug_print_backtrace ()
As you can see from the name, this method prints the backtracking content directly, and its function declaration is the same as debug_backtrace (), but $options defaults to DEBUG_BACKTRACE_IGNORE_ARGS, that is, it only prints the file and the number of lines in which the call is made.
Function a () {
B ()
}
Function b () {
C ()
}
Function c () {
Debug_print_backtrace ()
}
A ()
# 0 c () called at [/ Users/zhangyue/MyDoc/ blog post / dev-blog/php/202004/source/PHP print tracking debugging information .php: 144]
# 1 b () called at [/ Users/zhangyue/MyDoc/ blog post / dev-blog/php/202004/source/PHP print tracking debugging information .php: 140]
# 2 a () called at [/ Users/zhangyue/MyDoc/ blog post / dev-blog/php/202004/source/PHP print tracking debugging information .php: 151]
In addition, this function does not need to use var_dump () or print_r () for output, just use this function for output. It is very quick and easy for us to debug. For example, in a large framework like laravel, we can use debug_print_backtrace () to quickly view the current stack calls when the controller needs to view stack information. If debug_backtrace () does not specify $options, it will take up a very large amount of memory or cannot be fully displayed.
Summary
The two functions introduced today can flexibly help us debug our code or understand how a framework is called. Of course, in formal cases, it is recommended to use Xdebug plus editor support for breakpoint debugging, because we cannot see the changes in variables using the two methods debug_backtrace ().
Test the code:
Https://github.com/zhangyue0503/dev-blog/blob/master/php/202004/source/PHP%E6%89%93%E5%8D%B0%E8%B7%9F%E8%B8%AA%E8%B0%83%E8%AF%95%E4%BF%A1%E6%81%AF.php
Reference document: https://www.php.net/manual/zh/function.debug-backtrace.phphttps://www.php.net/manual/zh/function.debug-print-backtrace.php
At this point, the study on "how to print tracking and debugging information in PHP" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.