亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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)
{
//規(guī)定:檢查$func是否是函數(shù),如果不是函數(shù)停止執(zhí)行本段代碼,返回false
   if(!is_callable($func))
   {
    return false;
   }
   //把$one、$two相加,再把$one和$two傳入$func這個函數(shù)中處理一次
   //$func是一個變量函數(shù),參見變量函數(shù)
   echo $one + $two + $func($one,$two);
}
  //我們定義幾個函數(shù)試試
function plusx2($foo ,$bar)
{
$result = ($foo+$bar)*2;
return $result;
}
function jian($x, $y)
{
$result = $x - $y;
return $result;
}
//調(diào)用一下函數(shù),woziji,向里面?zhèn)魅雲(yún)?shù)試試
echo woziji(20,10,'plusx2');   // 輸出結(jié)果為 90
//將plusx2改成jian試試結(jié)果
echo woziji(20,10,'jian');   //輸出結(jié)果為 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變?yōu)榱薲emo,把demo后加上了一個括號,就執(zhí)行函數(shù)了 
$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)
{                
//規(guī)定:檢查$func是否是函數(shù),如果不是函數(shù)停止執(zhí)行本段代碼,返回false                
    if(!is_callable($func))
    {                
    return false;                
    }                
    //把$one、$two相加,再把$one和$two傳入$func這個函數(shù)中處理一次                
    //$func是一個變量函數(shù),參見變量函數(shù)                
    echo $one + $two + $func($one,$two);  //結(jié)果為: 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 '我是函數(shù)foo喲,調(diào)一下我才會執(zhí)行定義函數(shù)bar的過程<br />';
   function bar()
   {
     echo '在foo函數(shù)內(nèi)部有個函數(shù)叫bar函數(shù)<br />';
   }
 }
 //現(xiàn)在還不能調(diào)用bar()函數(shù),因為它還不存在
 bar();
 foo();
//現(xiàn)在可以調(diào)用bar()函數(shù)了,因為foo()函數(shù)的執(zhí)行使得bar()函數(shù)變?yōu)橐讯x的函數(shù)
 bar();
//再調(diào)一次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


Continuing Learning
||
<?php function demo() { echo '天王蓋地虎'; } function test() { echo '小雞燉蘑菇'; } $fu = 'demo'; //把$fu變?yōu)榱薲emo,把demo后加上了一個括號,就執(zhí)行函數(shù)了 $fu(); // 輸出為 天王蓋地虎 //把$fu的值改為test字符串再試試? ?>
submitReset Code