Examples of Debug_backtrace, debug_print_backtrace, and anonymous function usages in PHP _php tips

Source: Internet
Author: User

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.

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.