Home >Backend Development >PHP Problem >There are several types of php functions

There are several types of php functions

王林
王林Original
2019-10-15 13:15:194525browse

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".&#39;=&#39;.($m+$n);
}
//按名调用
echo add(20,33);
echo &#39;<hr>&#39;;
 
//2.匿名函数
//匿名并非无名,而是指名称可以任意指定,非常适合用一个变量来引用
$mult = function($m,$n)
{
    return "$m*$n".&#39;=&#39;.($m*$n);
};
echo $mult(21,25);
echo &#39;<hr>&#39;;
//匿名函数的本质就是一个值,只不过这个值里面保存的是一个函数的定义
//匿名函数最重要的两个用途:回调函数,闭包
 
 
//3.自调用函数(自执行函数),也不需要名称,算是匿名函数的一个变种
//$sub = function($m,$n)
//{
//    return "$m-$n".&#39;=&#39;.($m-$n);
//};
echo (function($m,$n)
{
    return "$m-$n".&#39;=&#39;.($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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn