The functions in PHP look very simple, but in fact they are very powerful. I can divide them into the following three categories according to whether the function name is fixed:
1. Functions with fixed names:
This type of function, also called: regular function, is created directly using the keyword function. It is also the most familiar type. For example:
<?php //1按名调用:最直接和普通的方式 echo '1.用function关键字创建常规函数,按函数名称调用:<br>'; function add($a, $b){ return $a.'+'.$b.'='.($a+$b); }; echo add(5,6); echo '<hr>';
Execution returns:
1. Use function Use keywords to create regular functions and call them by function name:
5+6=11
2. Functions with dynamically set names:
This type of function has many names, such as: anonymous Functions, variable functions, variable functions, closure functions, etc. are actually all the same. Don’t be intimidated by the names.
This kind of dynamically set function is called using the variable name that references this function, so it is very suitable for use in callback functions~~
For teaching purposes, I will collectively refer to it here as: Anonymous function.
There are two main ways to create anonymous functions. Some textbooks talk about them separately, so I will separate them here.
1. Creation method 1: Use PHP’s built-in create_function (parameter list, function body) function to complete. This function has two parameters. These two parameters must be placed in quotation marks. Don’t ask why, that’s how PHP is. According to the regulations, if you are not satisfied, then you can create a programming language~~
<?php //2.用系统函数create_function()来创建一个匿名函数/可变函数/变量函数/闭包函数 echo '2.用内置create_function()函数创建匿名函数,用变量名调用:<br>'; $func1 = create_function('$a,$b','return $a.\'+\'.$b.\'=\'.($a+$b);'); echo $func1(5,6); echo '<hr>';
Execution returns:
2. Use the built-in create_function() function to create an anonymous function and call it with the variable name:
5+6=11
2. Creation method two: Use the function keyword to create. The creation process is the same as a regular function. The difference is that it appears at the position of the value, that is, '= 'The right side of the equal sign. Assign the entire created function to a variable;
<?php //3匿名函数:也叫闭包函数,将函数做为值赋给一个变量 echo '3.用function关键字创建匿名函数,用变量名调用:<br>'; $func2 = function ($a, $b){ return $a.'+'.$b.'='.($a+$b); }; echo $func2(5,6); echo '<hr>';
Execution returns:
3. Use the function keyword to create an anonymous function and call it with the variable name:
5+6=11
3. There is no function name. After creation, you can directly pass the parameters and call the execution:
This type of function is similar to the anonymous function, but it is more rough. After creation, just wrap it with parentheses. It can be called after passing in the parameters, so this type of function does not need a name at all and is a one-time function. Just like a disposable item, throw it away after use.
<?php//4自执行函数:创建完立即执行不必明示调用echo '4.用function关键字创建匿名函数,然后直接传参调用执行:<br>';//自运行的匿名函数echo (function ($a,$b){return $a.'+'.$b.'='.($a+$b);})(5,6);
Execution return:
4. Use the function keyword to create an anonymous function, and then directly pass the parameters and call the execution:
5+6=11
Summary (very important, must read):
In PHP, the use of functions is still very flexible, but no matter what, functions, in the final analysis, are still a process. At the same time, there must be a return value so that users can perceive its existence and its value!
So, a function is always used as a value. Therefore, it cannot appear on the left side of the equal sign '='. This is the biggest difference between functions and language structures.
For example, we often say: echo() and list() are a language structure, not a function. Although they look like functions, it is because they can be placed on the left side of the equal sign and are acceptable. Assignment. But functions don't work. I hope this article will give you a deeper understanding of functions~~
Source of the article: http://peter.php.cn/blog/detail/188.html