Home  >  Article  >  Backend Development  >  Types and calling methods of PHP functions revealed

Types and calling methods of PHP functions revealed

天蓬老师
天蓬老师Original
2017-04-27 11:29:292261browse

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, which is also the most familiar type to everyone, for example:

<?php
//1按名调用:最直接和普通的方式echo 
&#39;1.用function关键字创建常规函数,按函数名称调用:<br>';
function add($a, $b){return $a.'+'.$b.'='.($a+$b);};
echo add(5,6);
echo '<hr>';

Execution returns:

1.用function关键字创建常规函数,按函数名称调用:
5+6=11

2. Name Dynamically set functions:

This type of function has many names, such as: anonymous function, variable function, variable function, closure function, etc. In fact, they are all the same. Don’t be intimidated by the name.

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 one: 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. If you are not satisfied with the regulations, then you can create a programming language~~

//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.用内置create_function()函数创建匿名函数,用变量名调用:
5+6=11

2. Creation method two: Use the function keyword to create, the creation process is the same as the routine The function is the same, the difference is that it appears in the position of the value, that is, on the right side of the '=' equal sign. Assign the entire created function to a variable;

//3匿名函数:也叫闭包函数,将函数做为值赋给一个变量echo
'3.用function关键字创建匿名函数,用变量名调用:<br>';
$func2 = function ($a, $b){return $a.'+'.$b.'='.($a+$b);};
echo $func2(5,6);
echo '<hr>';

Execution returns:

3.用function关键字创建匿名函数,用变量名调用:
5+6=11

3. There is no function name. After creation, directly pass parameters and call execution:

This type Functions are similar to anonymous functions, but are more crude. Once created, they are wrapped in parentheses and passed in parameters before being called. Therefore, 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.

//4自执行函数:创建完立即执行不必明示调用
echo '4.用function关键字创建匿名函数,然后直接传参调用执行:<br>';
echo (function ($a,$b){return $a.'+'.$b.'='.($a+$b);})(5,6);  
//自运行的匿名函数

Execution returns:

4.用function关键字创建匿名函数,然后直接传参调用执行:
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, it is 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~~

The above is the detailed content of Types and calling methods of PHP functions revealed. 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