Home  >  Article  >  Summary of php7.0 function types and calling methods

Summary of php7.0 function types and calling methods

PHP中文网
PHP中文网Original
2017-07-28 15:26:342266browse

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:

';
function add($a, $b){
  return $a.'+'.$b.'='.($a+$b);
};
echo add(5,6);
echo '
';

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~~

';
$func1 = create_function('$a,$b','return $a.\'+\'.$b.\'=\'.($a+$b);');
echo $func1(5,6);
echo '
';

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;

';
$func2 = function ($a, $b){
  return $a.'+'.$b.'='.($a+$b);
};
echo $func2(5,6);
echo '
';

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.

';//自运行的匿名函数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 

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