PHP Knowledge points-method functions

Source: Internet
Author: User

First, basic use
1. function definition Form
Function name (parameter 1, parameter 2, ...)
{
function body (code block)
}?
function call Flow Analysis
o Start call: actual parameter data to form parameter
o The program execution process enters the function (a separate run space), which is isolated from the global execution space
o Execute code in functions according to normal program logic
o If a return statement is encountered, the execution of the function is terminated, and the position of the function start call is skipped;
o If you execute to the last position of the function, you also jump back to where the function started calling

2. function Parameter Problem
In the definition, there are formal parameters (formal 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 only valid when the function is called and executed, the function ends, and these variables are usually "destroyed".
When called, there are actual arguments (arguments).
An argument is an "actual data",
This data can be a "direct data" (such as 5, "abc"), or it can be a variable stored in the data.
The effect of an argument is to "assign" its data to a parameter variable.
There should usually be a "one by one correspondence" relationship between the arguments and the parameters:
Definition form: Function name (parameter 1, parameter 2, ...) { 。。。。 }

Invocation form: Function name (argument 1, Argument 2, ...). )
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

3, the number of parameters problem
1, the number of arguments to a function can be 0 or more--how many, not grammatical problems, but the application of a problem.
2, in general, the number of actual parameters should be the same as the number of formal arguments.
3, however, on the basis of 2, if the formal parameter is in the default value, then the corresponding item of the actual parameter can be omitted.
That is, the number of arguments should be at least not less than the number of non-default parameter parameters in the formal parameter.
But:
We also have a special handling function parameter usage: number of free parameters
A parameter can be defined without a parameter, but it can be given any number of arguments when called.
In the system, the Var_dump () function also has the same effect:
Var_dump ($v 1);
Var_dump ($v 1, $v 2, $v 3);
The implementation of this application is achieved by relying on 3 system functions in the system:
Func_get_args ();//Get all the argument data received by a function, and the result is an array
Func_get_arg (n);//Gets the nth argument data received by a function (n starts from 0)
Func_num_args ();//Gets the number of all the argument data received by a function

4. Return value of function:
Typically, the data returned by the function is returned as a "value pass": The value of the variable in the function "copies" a copy, and then returns the corresponding code (assignment, output, calculation) to the received location.
But:
We can also let the value of the variable data in the function return as a "reference pass":
The form is as follows:
To define a function:
Functions & Function names (parameter 1, parameter 2, ...) Note that there is a reference symbol "&" in front of the function name
{
$result = 0;//Initialization
return $result;//returns data at this time, only variables
}
Call Function:
$v 1 = & function name (argument 1, Argument 2, ....) );//refers to the returned function, which naturally means a return value.

Other forms of the function
1 variable function
mutable function, which is the function name "variable" --in fact, as with variable variables.
$str 1 = "F1";//is just a string with the content "F1"
$v 1 = $str 1 (3, 4);//The form looks like a variable followed by parentheses, then it is essentially the "content" (F1) of the variable, which is called the function F1 (3, 4);
2 anonymous functions An
anonymous function is a function that has no name and has two representations:
Performance 1:
$f 1 = function () {... function body;};
//The anonymous function definition here does not have a name, but in fact assigns it to the variable $f1
when used, it is the same as "variable function": $v 1 = $f 1 ();
Representation 2:
Calls other functions 2 (anonymous function, argument 1, Argument 2, ...).
Description:
1 This form of anonymous function only defines the function body (no function name)
2 This form of anonymous function can only be used as a parameter when other functions are called (other functions often have specific uses)
3 This anonymous function is executed in the process of calling other functions. There are not many functions that the
can use (anonymous) functions as arguments!
One of the common ones is: Call_user_func_array (); The
is used as:
Call_user_func_array (anonymous function, array);
Meaning:
passes each item of an array to the anonymous function as several arguments to the anonymous function, executes the anonymous function, and can return data from the anonymous function.

Iii. Scope of variables
Local scope: can only be used within the scope of the defined function.
Global scope: Used in the "outside" scope of the function.
In--php, local and global scopes are non-overlapping
--JS, the global scope is the one that includes the local scope
Hyper-Global: can be used both inside and outside the function.
Super Global variables only those predefined within the system, we can no longer create super-global variables in the program.
Static local scope: In fact, it is also partial, but one more characteristic: The data can remain unchanged after the function exits.

Specific syntax for local access to global variables
1, in a local scope, use the global keyword to make a "declaration" of the globals, then you can use:
Syntax: global $ variable name;
2, in the function (local scope), use the $globals Super Global Array to refer (use) Global variables:
$GLOBALS a Hyper-global array is used to store data for all global variables: The variable is named subscript and the value of the variable is the corresponding element value.
However, by $GLOBALS manipulating global variables, it is a direct operation (not a reference operation), that is, if the corresponding element is unset, the corresponding variable of the global variable is also unset:
3, in fact, we can also use the $globals array directly inside the function, adding elements to create global variables, and naturally similar to the local use of the global:

Global access to specific statements for local variables
? Passing a reference to a parameter by reference passing a real parametric
O$V1 = 10;
Ofunction F1 (& $p 1, $p 2) {...} $p 1 is the formal parameter of the function, that is, the internal (local) variable of the function
O$v2 = F1 ($v 1, 10);//At this point we think $V1 can use the value $P1 in the function.
? Use the function's Reference return form: See the way the previous reference passed to return data
? The Global keyword is used in the function to refer to a globally variable for the first time, and the variable is available at the global scope after the function ends.

system functions related to functions:
? Function_exists (): Determines whether a function is defined, returns a Boolean value
OIF (Function_exists ("func1") = = False) {
? function Func1 () {... };//defining functions
O
? Func_get_arg (N): Gets the nth argument value of a function (n starts from 0)
? Func_get_args (): Gets all the arguments of a function, the result is an array
? Func_num_args (): Gets the number of all arguments for a function.

Iv. programming Ideas about functions
Recursive thinking (recursive function)
Recursive thinking (iterative thinking)

PHP Knowledge points-method functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.