Home >Backend Development >PHP Tutorial >Introduction to PHP functions_PHP tutorial

Introduction to PHP functions_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:04:38882browse

Introduction to PHP functions

Basic usage:
Function definition form:
function function name (formal parameter 1, formal parameter 2,...) {
//Function body (code block);
Function call form:
Essentially, it is to use a name to achieve the function of executing the function. It can usually be called in two situations:
The first type: a function without a return value, the calling statement is an independent statement
Function name (actual parameter 1, actual parameter 2,...); //The number of actual parameters should match the formal parameters
The second type: for a function that returns a value, the calling statement is usually "mixed" in other statements, and the calling statement is used as a "data":
A: $v1 = function name(); //Assign to other variables, the actual parameter syntax is omitted here, the same below
              B: $v1 = function name()*3 6; //Participate in the operation and then assign the value
C: echo function name(); //Output directly
D: echo function name()*3 6; //Participate in the operation and then output
E: $v1 = function name 2 (function name (), actual parameter 2, actual parameter 3,...); //Use as actual parameter
Function calling process analysis:
Start calling: actual parameters pass data to formal parameters
The program execution flow enters the function (an independent running space), which is isolated from the global execution space
Execute the code in the function according to regular program logic
If a return statement is encountered, the execution of the function will be terminated and jump back to the location where the function was called
If the execution reaches the last position of the function, it will also jump back to the position where the function was first called
The schematic diagram of its operation process is as follows:
Introduction to PHP functions_PHP tutorial
Function parameter problem:
A function,
When defining, there are formal parameters (formal parameter: parameter)
1: The formal parameter must be a variable name,
2: The variable name can only be a valid variable name in the function,
3: And it is only valid when the function is called and executed. When the function ends, usually these variables are "destroyed"
There are actual parameters when calling (actual parameter: argumentet)
The actual parameter is an "actual data", which can be a "direct data" (such as 5, "abc"), or data stored in a variable.
The function of the actual parameter is to assign its data to the formal parameter variable
There should usually be a “one-to-one correspondence” relationship between actual parameters and formal parameters
Default value parameter:
When defining a function, you can set a default value for the formal parameter in the position of the formal parameter, which can be called a default value parameter. For example:
Introduction to PHP functions_PHP tutorial
Note:
The default value cannot be an object or resource type
The default value can only be a constant expression or constant, not a variable
That is: the following syntax is correct: function f1($v = 3) {}, function f1($v = __LINE__) {},
The following syntax is incorrect: function f1($v = 3 1) {}, $m = 3,function f1($v = $m) {},
Problems with function parameter value transfer:
In fact, the problem of parameter value transfer of functions is the same as the value transfer between variables: the default is value transfer.
If the actual parameter itself is "direct data", there is no value transfer problem, but a simple "assignment"
The problem of passing by value only occurs when the actual parameter is a variable:
Introduction to PHP functions_PHP tutorial
We can also pass a parameter (formal parameter) by reference:
The formal parameter (actual parameter) passed by reference will change its value inside the function, and the actual parameter outside the function will also be modified accordingly
Introduction to PHP functions_PHP tutorial
Note: If a formal parameter is set to be passed by reference, at this time, the actual parameter can only use variables, otherwise a syntax error will occur, such as:
Introduction to PHP functions_PHP tutorial
The problem with the number of parameters:
1: The number of parameters of a function can be 0 or more - the specific number is not a syntax issue, but an application issue
2: Generally, the number of actual parameters should be consistent with the number of formal parameters
3: However, based on the second article, if there are default values ​​in the formal parameters, the corresponding items of the actual parameters can be omitted
That is: the number of actual parameters should at least be no less than the number of non-default value parameters in the formal parameters
But: we also have a special way to deal with function parameters: the number of free parameters
No formal parameters can be given when defining, but any number of actual parameters can be given when calling
In the system, the var_dump() function also has the same effect:
$var_dump($v1);
var_dum($v1,$v2,$v3); //This is also possible
The implementation of this application relies on three system functions in the system:
func_get_args(); //Get all the actual parameter data received by a function, and the result is an array
func_get_arg(n); //Get the nth actual parameter data received by a function (n starts from 0)
func_num_args(); //Get the number of all actual parameter data received by a function

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1072632.htmlTechArticleIntroduction to the basic use of PHP functions: Function definition form: function function name (formal parameter 1, formal parameter 2,... .) { //Function body (code block); } Function call form: Essentially, use a...
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