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 function is used by default with local variables, if you need to use global variables in the function, you need to use the global keyword to introduce the use of variables; 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 of the declaration, can be used directly 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 ($ a1,& $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: Using the static keyword declaration, 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 (ten);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 (30); the default parameter for the//$b is 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 lists (arrays) 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 the variable function to customize the callback function; 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, Write directly to the 2nd-multiple parameters, which is 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 anonymous function {$a}<br/>";
}; Declaring an anonymous function behind a function body;
$func (10);
Var_dump ($func);
Example: Calculate a number of classes:
function Jiec ($num) { static $jie =1; // function is not released immediately after execution if ($num >0) { $jie*= $num; // 3 Jiec (-$num); } return $jie;} Echo Jiec (ten);
[recursive function]
function func ($num) {echo $num. " <BR/> " ; if ($num >0 -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 a lot of commonly used functions, will provide the execution of the writing, eg: function notation Echo ("111"), the instruction of the wording echo "111";
If you feel good, move your finger and give it a little bit of praise! You will transfer YO!!!
function declaration and use in PHP