Advanced usage of PHP custom functions
Callback function
The callback function can be used with anonymous functions and variable functions to achieve a more beautiful and complex function structure.
The callback function is to make this function more customizable when processing a function. When this function is allowed to be called, a function can also be passed in to cooperate and assist in processing.
The processing process is as follows:
Assign 20 to the formal parameter $one, 10 to $two, and the two variable functions plusx2 or jian are assigned to $func
In the woziji function, determine whether plusx2 or jian is a function. If it is not a function, return false and stop execution.
Display plusx2 Or jian is a function. Therefore, $one = 20, $two =10 are added. After the addition, $one and $two are brought into $func($one,$two).
After bringing it in, $func is variable and can be plusx2 or jian. If it is plusx2, the two results of $one = 20, $two = 10 are given to $foo and $bar in the plusx2 function
$foo + $bar multiplied by 2. After 2, return the result to the calculation point of woziji function body: $one + $two + $func($one,$two);
In this way, the main result of the calculation is obtained
Now we understand the callback function: in a callback, pass in a function name and add () brackets to the function name. Recognize it as a variable function and execute it together.
Variable function
Variable function, we will also call it variable function
The usage of variable function is like this :
Anonymous function
Sometimes a function is just for us For some temporary processing, there is no need to reuse the function. It is very troublesome to give a name, so an anonymous function is needed to handle it.
Because the anonymous function has no name, if you want to use it, you need to return it to a variable.
The first usage of anonymous functions is to directly assign the assignment value to the variable, and calling the variable is to call the function.
Variable functional anonymous function
Callback anonymous function
Internal Function:
Internal function means that a function is declared inside the function.
Note:
The internal function name cannot be an existing function name
Assuming that an internal function is defined in function a, function a cannot be used twice .
Let’s look at the code below, you will learn it quickly:
'; function bar() { echo '在foo函数内部有个函数叫bar函数
'; } } //现在还不能调用bar()函数,因为它还不存在 bar(); foo(); //现在可以调用bar()函数了,因为foo()函数的执行使得bar()函数变为已定义的函数 bar(); //再调一次foo()看看是不是会报错? foo(); ?>
You will find that a bar function is defined inside the foo() function above, which is the inner function number .
After careful observation and experimentation, we will draw the following conclusions:
1. Calling foo() twice will report an error
2. The bar function cannot be executed without calling the foo() function, because bar is inside foo