Php debug_backtrace, debug_print_backtrace, and anonymous Functions

Source: Internet
Author: User

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 ();


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.