Home  >  Article  >  Backend Development  >  What is php anonymous function? Introduction to the use of php anonymous functions

What is php anonymous function? Introduction to the use of php anonymous functions

不言
不言Original
2019-01-09 14:00:144282browse

Anonymous functions are also called closures. This function can be called without specifying a name. In this article, we will introduce how to use PHP anonymous functions.

What is php anonymous function? Introduction to the use of php anonymous functions

Note: PHP anonymous functions are compatible with PHP5.3 or higher, that is to say, smooth PHP anonymous functions cannot be used in earlier versions.

Described as follows.

function(参数){
 //处理 
 };

Usually there is a function name after function, but this is not the case for anonymous functions. Parameters can be passed just like ordinary functions.

Next, let’s look at the use of anonymous functions

Let’s first look at the advantages of using anonymous functions

Preventing inconsistencies in function names

If you develop, the larger the scale of development, the greater the number of functions.

The naming of many small function functions may make the code very confusing.

Also, it is difficult to think of names related to non-repeating function names and functions.

Since anonymous functions are only used in certain situations, there is no need to name them.

Specify callback function

Use anonymous function, you can also use callback function.

When calling another function during the processing of a certain function or after the processing is completed, hand over another function in advance.

At this time, another function passed to the function is called the callback function.

Let’s take a look at the specific usage of php

Pass the value to the parameter of the anonymous function

<?php
//程序1
$course = function($test_val){
echo($test_val. "VIP课程");
};
//程序2
$course("php中文网");

In [Program 1], the variable $course is assigned to an anonymous function. Simply assigning the anonymous function to the variable $course does not execute it.

Execute the anonymous function assigned to $course in [Program 2]; when executing, pass the value "php Chinese network" to the anonymous function; in the anonymous function, the value "php Chinese network" will in $test_val.

The execution result is: php Chinese network VIP course

The passed value is executed and displayed in the anonymous function.

Passing a function as an argument to an anonymous function

Below is a sample code that passes a function (callback function) as an argument to an anonymous function.

<?php
//描述1
function no_name_callback(){
    return "php中文网";
}
//描述2
function no_name($callback){
    echo $callback(). "VIP课程";
}
//描述3
no_name("no_name_callback");

The running result is: php Chinese network VIP course

The callback function is executed and the value is displayed.

The above is the detailed content of What is php anonymous function? Introduction to the use of php anonymous functions. For more information, please follow other related articles on the PHP Chinese website!

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