Home >Backend Development >PHP Problem >There are several types of php functions
PHP function classification
1. Standard function, also called named function, ordinary function
2. Anonymous function, This is the key point, and it is also the most commonly used form in development. It is mainly used in callbacks and closures
3. Self-calling functions will be executed immediately after writing
Example :
<?php //1.普通函数 function add($m,$n) { return "$m+$n".'='.($m+$n); } //按名调用 echo add(20,33); echo '<hr>'; //2.匿名函数 //匿名并非无名,而是指名称可以任意指定,非常适合用一个变量来引用 $mult = function($m,$n) { return "$m*$n".'='.($m*$n); }; echo $mult(21,25); echo '<hr>'; //匿名函数的本质就是一个值,只不过这个值里面保存的是一个函数的定义 //匿名函数最重要的两个用途:回调函数,闭包 //3.自调用函数(自执行函数),也不需要名称,算是匿名函数的一个变种 //$sub = function($m,$n) //{ // return "$m-$n".'='.($m-$n); //}; echo (function($m,$n) { return "$m-$n".'='.($m-$n); })(30,20); ?>
Recommended tutorial: PHP video tutorial
The above is the detailed content of There are several types of php functions. For more information, please follow other related articles on the PHP Chinese website!