This article mainly introduces the function declaration and use in PHP, the need for friends can refer to the following
Function
1. Function name is one of the identifiers, can only have alphanumeric underline, the beginning can not be a number;
The naming of function names must conform to the "small hump rule"FUNC(),func(),Func();
Function names are case-insensitive;
The function name cannot have the same name as the existing function, and cannot have the same name as the built-in function;
2. function_exists("func");
used to detect whether a function has been declared;
Note that the passed function name must be in string format and return the result as True/false;
When echo prints, true is 1,false not displayed;
[Scope of variables in PHP]
1. Local variables: Variables declared inside the function, known as local variables, are used only inside the function, outside the function to use, you need to use the return keyword in the function;
2. Global variables: Variables declared outside the function, called global variables;
3. The use of variables in the (more commonly used) function uses local variables by default, and if you need to use global variables in a function, you need to use the Global keyword to bring in the use of globals;
The name of the variable in the function, if it is duplicated with the global variable name, global, is the function's local variable, and globally it is the function;
4. $GLOBALS ['] global array;
$GLOBALS [' A3 '] array, PHP gives us a built-in global array, you can directly add values to the array, whether inside or outside the function declaration, can be directly used anywhere; eg: $GLOBALS [' A3 '] = 10;
5. There is also a way to use global variables in a function: by passing parameters to a parameter, you can use global variables inside the function, but the passed arguments are local variables, internal changes, and the external does not change unless the argument passed is the address. function func ($a 1,& $a 2) {} Func ($a 1, $a 2);
(reason) $a 1 is a local variable, internal change, external will not change, $a 2 is also the internal variable address, internal changes, external changes;
If the function's formal parameter has a fetch address symbol, then when the function is called, the argument must be a variable, not a literal;
Eg:func ($a 1, $a 2) pair of func ($a) wrong
"Static variables"
Static variables: declared with the static keyword, static $num = 10;
Characteristics of static variables:
Static variables are declared when the function is first loaded;
Static variables are not released immediately after the function is used, and static variables are declared only once throughout the execution of the script;
The same function is called multiple times, sharing the same static variable.
[parameter passing method of function]
In PHP, the number of arguments can only be more than formal parameters, can not be less than formal parameters, or will be error
1. General parameter passing:
function Fun ($a) { $a +=10; return $a; } echo Fun (10);
2. Parameters of the reference type:
$a =10; function func (& $a) { $a +=10; } Func ($b);
Reference parameter passing, function internal modification variable, function outside synchronization change;
Parameters are reference arguments, and arguments can only be variables, not literals.
3. Default parameters:
function func ($a, $b =10) { return $a + $b; } echo func (+); The default parameter for $b is 10
If the parameters have both default parameters and non-default parameters, the default parameter list must be followed by a non-default parameter list, that is, to guarantee the order of assignment of non-default parameters.
Func_get_args (); Take all parameter list (array) Func_num_args (); The total number of all parameters, equal to count (Func_num_args ()); Func_get_arg (0); According to the following table, take each parameter
[variable function]
Assigns a function name to a variable after it is converted to a string. This variable, which is what we call the variable function, can be added () to invoke the function content;
function func () {}---->fun= "func",----->func ();
[callback function]
1. Use variable functions, custom callback functions
function ($func) {func ();} -->function f () {}--->func ("f");
2 use Call_user_func_array and call_user_func to customize callback functions;
The first parameter of the two function is a callback function, which indicates the execution of the current callback;
The difference is that: Call_user_func_array () The second parameter is an array and assigns each value of the arrays to the parameter list of the callback function, which is equivalent to the Apply () in JS; And, Call_user_func, is the parameter list of the callback function, directly expand to write to the 2nd-multiple parameters, equivalent to the call () in JS;
Eg:call_user_func_array ("func", Array (n/a));--->func (a);
Call_user_func ("func");---->func (a);
[anonymous function]
Since variable functions have multiple invocations at the time of Invocation, $fun ()/func () so that an anonymous function is generated in order for the function to be called more uniformly.
Declaring an anonymous function behind the body of a function; Essential!!!
The anonymous function itself is also a variable, which is detected as object type by Var_dump;
General functions:
function func () { $fun = "Func"} $fun ();//func ();
Anonymous functions:
$func =function ($a) {echo "I am an anonymous function {$a}<br/>"; }; Declaring an anonymous function behind the body of a function; essential $func (10); Var_dump ($func);
Example: Calculate a number of classes:
function Jiec ($num) { static $jie =1; The if ($num >0) { $jie *= $num is not released immediately after the function is executed; 3 Jiec (--$num); } return $jie;} Echo Jiec (10);
[recursive function]
Refers to the operation of the function itself inside the function, when the outer function body, encountered its own function calls, continue to enter the inner function execution, and the latter part of its own function is not executed, know that the most inner function after the execution, in the gradual outward execution;
function func ($num) { echo $num. <br/> "; if ($num >0) { func ($num-1); Func (--$num); Try a different result yo! //func ($num-); } echo $num. " <br/> "; }func (10);
[Include/require]
1. The role of both is to introduce external PHP files into the current file: include ' a.php '; include (' a.php ');
2. The difference between the two: (for wrong handling) when a file error is introduced, include generates a warning and does not affect subsequent code execution, and require generates an error, and subsequent code is no longer executed;
3. Generally when used to import some files at the top of the file, use require import, if it fails, then do not execute the file;
If the import performs certain operations in some branch conditions, the error does not affect the execution result.
4.include_once and require_once indicate that the file can only be imported once, and if the function is called multiple times, subsequent files will determine whether the file is imported and then decide whether to import the new file.
(Detecting whether a file is imported only concerns whether the file has been imported or not, and how it is imported.) )
5.include/require can import various types of files, which is equivalent to copy a copy of the current file, but during the copy process, the PHP engine compiles properly to ensure that there are no errors.
6.include and require are functions as well as instructions! PHP for many commonly used functions, will provide the execution of the writing, eg: the function of Echo ("111"), the instruction to write echo "111";