1. Define and call functions
Function concept: Write some repeatedly used functions in an independent code block and call them separately when needed.
The basic syntax format for creating a function is: Keywords that must be used when defining function .
Fun_name: is the name of the custom function.
$str1...$strn: are the parameters of the function.
Fun_body: It is the main body of the custom function and is the function implementation part.
Function call: After the function is defined, all you have to do is call the function. The operation of calling a function is very simple. You only need to quote the function name and assign the correct parameters to complete the function call. 2. Pass parameters between functions
When calling a function, you need to pass parameters to the function. The parameters passed in are called actual parameters, and the parameters defined by the function Parameters are formal parameters. There are three ways to pass parameters: pass by value, pass by reference and default parameters.
1. Pass by value
Copy the value of the actual parameter to the corresponding formal parameter. The operation inside the function is performed on the formal parameter. The result of the operation will not affect the actual parameter. That is, after the function returns, the actual parameter The value will not change.
2. Passing by reference
Passing by reference is to pass the memory address of the actual parameter into the formal parameter. At this time, all operations inside the function will affect the value of the actual parameter, and after returning, the value of the actual parameter will change. The way to pass by reference is to add an ampersand to the original value when passing by value. 3. Default parameters (optional parameters)
There is another way to set parameters, that is, optional parameters. You can specify a parameter as optional, place the optional parameter at the end of the parameter list, and specify that its default value is empty.
3. Return value from function
Usually, the way a function passes the return value to the caller is to use the keyword return().
Return() returns the value of the function to the caller of the function, that is, returns program control to the caller's scope. If the return() keyword is used in the global scope, the execution of the script will be terminated.
The return statement can only return one parameter, that is, it can only return one value, and cannot return multiple parameters at one time. If you want to return multiple results, you must define an array in the function, store the return value in the array, and return it.
4. Variable functions
PHP supports variable functions. The following uses an example to introduce the specific application of variable functions. (Omitted)
5. References to functions References can not only be used for ordinary variables,
function parameters, but also for the function itself. A reference to a function is a reference to the result returned by the function.
6. Cancel the reference
When the reference is no longer needed, you can cancel the reference. Dereference uses the unset() function, which only breaks the binding between the variable name and the variable content, rather than destroying the variable content.
The above is the detailed content of What is php calling function, parameter passing, variable function, reference. For more information, please follow other related articles on the PHP Chinese website!