


PHP function callback function (1) Variable function definition, declaration and application
The so-called Callback function means that when calling a function, instead of passing a standard variable as a parameter, another function is passed as a parameter to the called function. If callback type parameters appear in the format description of the function, the function is a callback function. The previous section introduced php variable parameter functions, this section will get into the study of PHP callback functions.
The format description of the callback function is as follows:
1 |
|
callback is also a type of pseudo-type in PHP, indicating that the parameters of the function need to accept another function as an actual parameter. A very important question is why use functions as parameters? As introduced before, the execution behavior of the calling function can be changed by passing parameters, but sometimes the ability to only pass a value to the function is still limited. If a user-defined execution process can be passed to the function for use, it greatly increases the user's expansion of the function functionality. How to declare and use callback functions is also a critical issue. If you need to declare a callback function, you need to first understand the variable function.
Variable function
Variable function is also called variable function. If a variable name is followed by parentheses, PHP will look for a function with the same name as the variable's value and will attempt to execute it. For example, declare a function test() and assign the function name string test to the variable $demo. If you print the $demo variable directly, the output value must be the string test; but if you add parentheses $demo() after the $demo variable, the function corresponding to the $demo variable value test is called. In this way, different function names can be assigned to the same variable, and then the function can be called through the variable, which is similar to the application of polymorphic features in object-oriented.
The example code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
In the above example, three functions one(), two() and three() are declared, using Used to calculate the sum, product and quotient of two numbers. And assign the function names of the three functions (without parentheses) to the variable $result as a string, then use the variable name $result followed by parentheses and pass in two integer parameters, it will Finds the function execution with the same name as the value of variable $result. Most functions can assign the function name to a variable to form a variable function. But variable functions cannot be used in language structures.
Use variable functions to declare and apply callback functions
If you want to customize a function that can be called back, you can choose to use variable functions Help achieve. When defining a callback function, the declaration structure of the function does not change, as long as the declared parameter is an ordinary variable. But when applying this parameter variable inside a function, if you add parentheses, you can call the function with the same name as the parameter value, so the parameter passed must be the name string of another function. The purpose of using the callback function is to pass a self-defined function to the function for use inside.
The code example is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
The result of running is:
In the above example , if the declared function filter() only accepts ordinary values as parameters, the number that the user can filter out will be relatively single. In this example, in the defined function filter(), a tax-containing value passed in as a filter condition is called. In this way, the function is much more powerful. You can filter out any numbers you don’t like in the filter() function. . By adding a parenthesis $fun() to the parameter variable $fun inside the function filter(), you can call a function with the same value as the variable $fun as a filtering condition. For example, in this example, two functions one() and two() are declared, which are used to filter out multiples and palindromes of 3 within 10 respectively. As long as the function names one and two are strings when calling filter() Passed to parameters, that is, these two functions are passed to the filter() function for internal use.
This chapter explains the variable function definition, declaration and application of the php callback function. In the next chapter we will introduce the call_user_func_array() function of the php callback function and its usage.
【Recommended related tutorials】
1. "php.cn Dugu Jiujian (4)-php video tutorial"
2. php programming from entry to master a full set of video tutorials
3. php practical video tutorial
The above is the detailed content of PHP function callback function (1) Variable function definition, declaration and application. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to optimize the lazy loading effect of images through PHP functions? With the development of the Internet, the number of images in web pages is increasing, which puts pressure on page loading speed. In order to improve user experience and reduce loading time, we can use image lazy loading technology. Lazy loading of images can delay the loading of images. Images are only loaded when the user scrolls to the visible area, which can reduce the loading time of the page and improve the user experience. When writing PHP web pages, we can optimize the lazy loading effect of images by writing some functions. Details below

The writing methods of java callback function are: 1. Interface callback, define an interface, which contains a callback method, use the interface as a parameter where the callback needs to be triggered, and call the callback method at the appropriate time; 2. Anonymous inner class callback , you can use anonymous inner classes to implement callback functions to avoid creating additional implementation classes; 3. Lambda expression callbacks. In Java 8 and above, you can use Lambda expressions to simplify the writing of callback functions.

How to reduce memory usage through PHP functions. In development, memory usage is a very important consideration. If a large amount of memory is used in a program, it may cause slowdowns or even program crashes. Therefore, reasonably managing and reducing memory usage is an issue that every PHP developer should pay attention to. This article will introduce some methods to reduce memory usage through PHP functions, and provide specific code examples for readers' reference. Use the unset() function to release variables in PHP. When a variable is no longer needed, use

Introduction to the basic writing and usage of Java callback functions: In Java programming, the callback function is a common programming pattern. Through the callback function, a method can be passed as a parameter to another method, thereby achieving indirect call of the method. The use of callback functions is very common in scenarios such as event-driven, asynchronous programming and interface implementation. This article will introduce the basic writing and usage of Java callback functions, and provide specific code examples. 1. Definition of callback function A callback function is a special function that can be used as a parameter

PHP image processing functions are a set of functions specifically used to process and edit images. They provide developers with rich image processing functions. Through these functions, developers can implement operations such as cropping, scaling, rotating, and adding watermarks to images to meet different image processing needs. First, I will introduce how to use PHP image processing functions to achieve image cropping function. PHP provides the imagecrop() function, which can be used to crop images. By passing the coordinates and size of the cropping area, we can crop the image

The performance of different PHP functions is crucial to application efficiency. Functions with better performance include echo and print, while functions such as str_replace, array_merge, and file_get_contents have slower performance. For example, the str_replace function is used to replace strings and has moderate performance, while the sprintf function is used to format strings. Performance analysis shows that it only takes 0.05 milliseconds to execute one example, proving that the function performs well. Therefore, using functions wisely can lead to faster and more efficient applications.

PHP functions have similarities with functions in other languages, but also have some unique features. Syntactically, PHP functions are declared with function, JavaScript is declared with function, and Python is declared with def. In terms of parameters and return values, PHP functions accept parameters and return a value. JavaScript and Python also have similar functions, but the syntax is different. In terms of scope, functions in PHP, JavaScript and Python all have global or local scope. Global functions can be accessed from anywhere, and local functions can only be accessed within their declaration scope.

The main differences between PHP and Flutter functions are declaration, syntax and return type. PHP functions use implicit return type conversion, while Flutter functions explicitly specify return types; PHP functions can specify optional parameters through ?, while Flutter functions use required and [] to specify required and optional parameters; PHP functions use = to pass naming Parameters, while Flutter functions use {} to specify named parameters.
