Home >Backend Development >PHP Tutorial >Using anonymous functions in php php function return value php array php date function

Using anonymous functions in php php function return value php array php date function

WBOY
WBOYOriginal
2016-07-29 08:53:171449browse

Old rule, code first and then talk. Using anonymous functions in php php function return value php array php date function

Output of the
<?php


function showArray($cols)
{
&#160; &#160; foreach ($cols as $obj) {
&#160; &#160; &#160; &#160; echo $obj . " ";
&#160; &#160; }
&#160; &#160; echo "<br>";
}


function showObj($obj)
{
    echo $obj . " ";
}


//mswap传入的是引用类型。
function mswap(&$a, &$b)
{
    $tmp = $a;
    $a   = $b;
    $b   = $tmp;
}


function bubbleSort(&$cols)
{
    $len = count($cols);
    for ($i = 0; $i < $len; $i++) {
&#160; &#160; &#160; &#160; for ($j = 1; $j < $len - $i; $j++) {
&#160; &#160; &#160; &#160; &#160; &#160; if ($cols[$j - 1] > $cols[$j]) {
                mswap($cols[$j - 1], $cols[$j]);
            }
        }
    }
}


$data = array(
    8,
    2,
    3,
    9,
    0,
    45,
    35,
    235
);
//排序
bubbleSort($data);
//打印数组
showArray($data);
//使用回调函数依次遍历打印数组
array_map('showObj', $data);
echo "<br>";
//使用匿名函数依次遍历打印数组
array_map(create_function('$obj', 'echo $obj." " ;'), $data);
?>
program:


The function of the code is very simple. It first initializes an array, then sorts it, and finally prints it out in three different ways.

The sorting algorithm uses the simplest bubble sorting, which will not be described in detail.

Things to note are:

(1) Both the sorting function and the swap function use the passing reference method. If you use the value-passing method, the sorting and swapping functions will not take effect, because at this time the function actually operates on a copy of the parameter object.

(2) array_map comes with PHP, and its function is to call the passed in function for each object in the array in turn. This method is called callback.

(3) create_function can create an anonymous function. In the last printed example, it is the anonymous function it creates that is used as an argument to array_map.

In fact, experienced programmers know that anonymous functions are inefficient in both C++11 and PHP. So why use anonymous functions? Why do the new standards of C# and Java also support it?

Because although famous functions seem intuitive, there are management costs. One more function means one more place to manage. And for a "micro" code fragment, writing a named function for it alone seems not compact enough. However, this does not mean that anonymous functions can be abused. When the code fragment is too large, using anonymous functions will seriously damage readability. The use of anonymous functions depends on the situation.

The above introduces the use of anonymous functions in PHP, including PHP and functions. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn