PHP functions. Basic usage of PHP functions: function definition form: function name (parameter 1, parameter 2 ,...) {function body (code block);} function call form: essentially an introduction to using a PHP function
Basic usage:
Function syntax:
Function name (parameter 1, parameter 2 ,...){
// Function body (code block );
}
Function call form:
In essence, a name is used to execute the function. Generally, there are two types of calls:
First, if a function does not return a value, the call statement is an independent statement.
Function name (real parameter 1, real parameter 2,...); // The number of real parameters should match the actual parameter
Type 2: For a function with a return value, the call statement is usually "mixed" in other statements and used as a "data:
A: $ v1 = function name (); // assign A value to other variables. the real parameter syntax is omitted here.
B: $ v1 = function name () * 3 + 6; // participates in the operation and then assigns a value.
C: echo function name (); // direct output
D: echo function name () * 3 + 6; // participates in the operation, and then outputs
E: $ v1 = function name 2 (function name (), real parameter 2, real parameter 3,...); // used as real parameter
Function call process analysis:
Start call: pass data to the form parameter of the actual parameter
The program execution flow enters the function (an independent runtime space), which is isolated from the global execution space.
Execute the code in the function according to the general program logic
If a return statement is run, the execution of the function is terminated, and the position where the function is called
If the execution is at the last position of the function, it also jumps back to the position where the function starts to be called.
The running process is illustrated as follows:
Function parameter problems:
A function,
A formal parameter (parameter: parameter)
1: The parameter must be a variable name,
2: the variable name can only be a valid variable name in the function,
3: The variable is valid only when the function is called and executed. the Function ends. Normally, these variables are "destroyed"
There are actual parameters when calling (real parameter: arguemet)
A real parameter is a "actual data", which can be a "direct data" (for example, "abc") or a data stored in a variable.
The role of a real parameter is to assign the value of its data to the parameter variable.
There should usually be a "one-to-one correspondence" relationship between real parameters and form parameters.
Default parameter:
When defining a function, you can set the default value for the form parameter at the location of the form parameter, which can be called the default value parameter. For example:
Note:
The default value cannot be an object or resource type.
The default value can only be a constant expression or a 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 ){},
Function parameter value passing:
In fact, the function parameter value passing is the same as the variable value passing rule (mode): The default value passing rule is used.
If the real parameter itself is "direct data", there is no value transfer problem, but a simple "value assignment"
The value passing problem only occurs when the real parameter is a variable:
We can also let a parameter (parameter) pass the value by reference:
Reference passed parameters (real parameters), change the value inside the function, and modify the real parameters outside the function.
Note: If a parameter is set to be passed as a reference, only variables can be used for the real parameter. Otherwise, a syntax error occurs, such:
Number of parameters:
1: The number of function parameters can be 0 or more-the specific number is not a syntax problem, but an application problem.
2: Generally, the actual number of parameters should be the same as the number of formal parameters.
3: However, based on the second entry, if the formal parameter has a default value, the corresponding items of the actual parameter can be omitted.
That is, the number of real parameters should be at least the number of non-default parameters in the form parameter
However, we also have a special function parameter usage: Number of free parameters
The parameter can be defined without being specified, but the real parameters of any number can be given during the call.
In the system, the var_dump () function has the same effect:
$ Var_dump ($ v1 );
Var_dum ($ v1, $ v2, $ v3); // This is also possible
The implementation of such an application depends on three system functions in the system:
Func_get_args (); // obtain all the real parameter data received by a function, and the result is an array
Func_get_arg (n); // Obtain the nth real parameter data received by a function (n starts from 0)
Func_num_args (); // gets the number of all real parameter data received by a function.
Basic usage of callback: function definition form: function name (parameter 1, parameter 2 ,...) {// function body (code block);} function call form: essentially,...