This article describes the usage of debug_backtrace, debug_print_backtrace and anonymous functions in PHP. Share it for your reference. The specific analysis is as follows:
debug_print_backtrace() is a very low-key function, and few people have noticed it.
However, when we call another object to another object and then another object and a function in the file makes an error, it is laughing.
debug_print_backtrace() can print out the calling process of a page, and it is clear where it comes from. However, this is a proprietary function of PHP5, and fortunately it has been implemented in the pearl.
1. debug_backtrace It can trace the calling information of the function. It can be said to be a debugging tool, and the code is as follows:
function one() { two(); }
function two() { three(); }
function three() { print_r( debug_backtrace() ); }
/*Output:
Array(
[0] => Array (
[file] => D:
[line] => 10
[function] => three
[args] => Array ( )
),
[1] => Array (
[file] => D:
[line] => 6
[function] => two
[args] => Array ( )
),
[2] => Array (
[file] => D:
[line] => 3
[function] => one
[args] => Array ( )
)
)*/
2. debug_print_backtrace The difference between it is that it will print backtracking information directly.
3. Anonymous functions
Since PHP 5.3, anonymous functions (Anonymous functions), also called closures (closures), the keyword use is also in anonymous functions.
Let’s first look at the example of anonymous function. As a parameter of the callback function, the code is as follows:
echo preg_replace_callback('~-([a-z])~', function ($match) {
return strtoupper($match[1]);
}, 'hello-world'
);
// Output helloWorld
?>
Keywords connecting closures and external variables: USE
The closure can save some variables and values of the code block context. By default, PHP cannot call the context variables of the code block, but needs to use the use keyword, and the code is as follows:
$num = 2;
$array = array(1,2,3,4,5,6,7,8);
print_r(array_filter($array, function($param) use ($num){
return $param % intval($num) ==0; })
);}
test();
I hope this article will be helpful to everyone's PHP programming.