This example describes the use of Debug_backtrace, Debug_print_backtrace, and anonymous functions in PHP. Share to everyone for your reference. The specific analysis is as follows:
Debug_print_backtrace () is a very low-key function, and few people have noticed it.
But when we call another object on one object and then call one of the other objects and a function in the file, it's laughing.
Debug_print_backtrace () can print a page of the call process, from where to where to go at a glance. But this is a PHP5 proprietary function, but it is already implemented in pear.
First, debug_backtrace it can backtrack trace function of the call information, can be said to be a debugging tool, the code is as follows:
Copy Code code as follows:
One ();
function one () {two (); }
function two () {three (); }
function three () {Print_r (Debug_backtrace ()); }
/* Output:
Array (
[0] => Array (
[File] => d:apmservwwwhtdocstestdebugindex.php
[Line] => 10
[Function] => three
[args] => Array ()
),
[1] => Array (
[File] => d:apmservwwwhtdocstestdebugindex.php
[Line] => 6
[function] => two
[args] => Array ()
),
[2] => Array (
[File] => d:apmservwwwhtdocstestdebugindex.php
[Line] => 3
[Function] => one
[args] => Array ()
)
)*/
Second, debug_print_backtrace it is different that it will print the backtracking information directly.
Third, anonymous function
The anonymous function (Anonymous functions), also called the closure function (closures), has been added from PHP 5.3, and the keyword use is also in the anonymous function.
Let's look at an example of an anonymous function as a parameter to the callback function, as follows:
Copy Code code as follows:
<?php
echo Preg_replace_callback (' ~-([A-z]) ~ ', function ($match) {
return Strtoupper ($match [1]);
}, ' Hello-world '
);
Output HelloWorld
?>
Keywords to connect closures and external variables: use
Closures can hold variables and values in the context of the code block in which PHP, by default, anonymous functions cannot invoke the context variables of the code block in which they reside, but by using the USE keyword, the code is as follows:
Copy Code code as follows:
function Test () {
$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 help you with your PHP program design.