Advanced usage ...LOGIN

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.

<?php
function woziji($one, $two, $func)
{
//规定:检查$func是否是函数,如果不是函数停止执行本段代码,返回false
   if(!is_callable($func))
   {
    return false;
   }
   //把$one、$two相加,再把$one和$two传入$func这个函数中处理一次
   //$func是一个变量函数,参见变量函数
   echo $one + $two + $func($one,$two);
}
  //我们定义几个函数试试
function plusx2($foo ,$bar)
{
$result = ($foo+$bar)*2;
return $result;
}
function jian($x, $y)
{
$result = $x - $y;
return $result;
}
//调用一下函数,woziji,向里面传入参数试试
echo woziji(20,10,'plusx2');   // 输出结果为 90
//将plusx2改成jian试试结果
echo woziji(20,10,'jian');   //输出结果为 40
?>

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 :

<?php
function demo()
{
echo '天王盖地虎';
}
function test()
{
echo '小鸡炖蘑菇';
}
$fu = 'demo';
//把$fu变为了demo,把demo后加上了一个括号,就执行函数了 
$fu();   // 输出为 天王盖地虎
//把$fu的值改为test字符串再试试?
?>


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

<?php
$greet = function($name)
{
echo $name.',你好';
};
$greet('明天');  //输出 明天,你好
$greet('PHP');   // 输出 PHP,你好
?>

Callback anonymous function

<?php                
function woziji($one,$two,$func)
{                
//规定:检查$func是否是函数,如果不是函数停止执行本段代码,返回false                
    if(!is_callable($func))
    {                
    return false;                
    }                
    //把$one、$two相加,再把$one和$two传入$func这个函数中处理一次                
    //$func是一个变量函数,参见变量函数                
    echo $one + $two + $func($one,$two);  //结果为: 150                
}                
woziji(20,30,function($foo,$bar)
{                
$result = ($foo+$bar)*2;                
return $result;                
}
);                
?>

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:

<?php
function foo()
{
  echo '我是函数foo哟,调一下我才会执行定义函数bar的过程<br />';
   function bar()
   {
     echo '在foo函数内部有个函数叫bar函数<br />';
   }
 }
 //现在还不能调用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


Next Section
<?php function demo() { echo '天王盖地虎'; } function test() { echo '小鸡炖蘑菇'; } $fu = 'demo'; //把$fu变为了demo,把demo后加上了一个括号,就执行函数了 $fu(); // 输出为 天王盖地虎 //把$fu的值改为test字符串再试试? ?>
submitReset Code
ChapterCourseware