This article we mainly share with you the PHP function example Introduction, the main simple introduction of the PHP function concept of the use of methods, I hope to help everyone.
Name: Follow the variable naming rules, functions cannot be overloaded,
function : functions implement structured programming and improve the maintainability of code
Global variables: can be used throughout the script.
Local variables: defined in the function body, only used within the function body, the function is automatically released after execution
It is divided into static storage type and dynamic storage type, static variable $a, not released after function execution.
Local variable programming global variables in function: 1.global; 2. $GLOBAL ['] Use global array
Types of functions
1. Functions for Custom parameters:
The function test ($a, $b) {}//test is the functional name, $a and $b are function arguments, and you need to provide the corresponding arguments when the function is called.
2. Functions that refer to parameters:
Function test (& $a) {}//$a is a reference variable, and the external variable is modified if $ A is modified inside the function relative to the external variable.
3. Functions for Default parameters:
Fuction Test ($a =0) {}//$a =0 means that when the function is called, the argument is not passed to the function, then the default $a=0;
4. Functions with variable number of parameters:
function test () {$a, $b,......} {}//principle: Accept all parameters and return a data use through the Func_get_args () function, so you can have multiple parameters
Func_get_args () Gets the parameter array, Func_num_args () Gets the number of arguments, Func_get_arg () returns the specified parameters (typically used in conjunction with Func_num_args)
5. Callback function: function one () {} $fun = "one"; When executing $fun (it is equivalent to invoking the one () function;
6. Recursive function: In the function body, in the call function itself, remember must have an exit, that is, the condition of exit, otherwise it will form a dead loop.
function test ($a) { if ($a >=0) { //exit recursive condition, be sure to have test ($a-1); } else{ return; } }
7. Anonymous function: is a function without a name, generally used for the callback function. Fucntion ($a) {}
Common functions:
Echo () "Language structure" print ()//"Language Structure" "has a return value", if the transmission fails to cause no output, it returns Falsevar_dump () Var_export () "has a return value, translates a valid PHP code" printf ()//similar to C The form of the language printf ("My name is%s, age%d", $name, $age), print out sprintf ()//similar to printf, but not print, but return formatted text, the rest is the same as printf