This article introduces debug_backtrace, debug_print_backtrace, and anonymous functions.
Debug_print_backtrace. In contrast, debug_print_backtrace directly prints backtracing information.
Debug_print_backtrace () is a very low-key function, which is rarely noticed.
However, when an error occurs when I call another object to call another object and a function in the object, it is smiling.
Debug_print_backtrace () can print out the call process of a page, from where to see clearly.
However, this is a PHP5 proprietary function. Fortunately, it has been implemented in pear,
1. debug_backtrace: It can trace the call information of a function. It can be called as a debugging tool.
| The Code is as follows: |
Copy code |
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 () ) )*/ |
2. debug_print_backtrace: different from debug_print_backtrace, debug_print_backtrace directly prints backtracing information.
Iii. Anonymous Functions
Since PHP 5.3, an Anonymous function (Anonymous functions) is added, also called a closure function (closures). The keyword use is also in an Anonymous function.
Let's take a look at the example of an anonymous function as a callback function parameter.
| The Code is as follows: |
Copy code |
Echo preg_replace_callback ('~ -([A-z]) ~ ', Function ($ match ){ Return strtoupper ($ match [1]); }, 'Hello-world' ); // Output helloWorld ?> |
Keyword for connecting closure and external variables: USE
The closure can save some variables and values in the context of the code block. By default, an anonymous function cannot call the context variable of the code block. Instead, you must use the use keyword.
| The Code is as follows: |
Copy code |
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 ();
|