Example of debug_backtrace, debug_print_backtrace, and anonymous function usage in php

Source: Internet
Author: User
This article mainly introduces debug_backtrace, debug_print_backtrace, and anonymous function usage in php. it analyzes the functions of debug_backtrace and debug_print_backtrace in the debugging process as an example, and analyzes the usage of new anonymous functions in PHP5.3, this document describes debug_backtrace, debug_print_backtrace, and anonymous function usage in php. Share it with you for your reference. The specific analysis is as follows:

Debug_print_backtrace () is a very low-key function, which is rarely noticed.

However, when an error occurs when we 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. it can be seen from where it came from. 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:

The code is 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 ()
)
)*/


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:

The code is as follows:

<? Php
Echo preg_replace_callback ('~ -([A-z]) ~ ', Function ($ match ){
Return strtoupper ($ match [1]);
}, 'Hello-world'
);
// Output helloWorld
?>


Keyword for connecting closure and external variables: USE

Closures can save some variables and values in the context of the code block. by default, PHP does not allow anonymous functions to call the context variables of the code block. Instead, you need to use the use keyword. the code is as follows:

The code is 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 PHP programming.

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.