Functions are divided into two kinds: system intrinsic function and user custom function. A function is a function that writes a piece of code or a function that is often used in everyday life. Called when needed, the purpose of calling the function is to simplify the burden of programming, reduce the amount of code and improve efficiency, to increase the reusability of code, to avoid duplication of development purposes.
1. Define and invoke functions, and how to pass values between functions.
The function is to write some reused functionality into a separate block of code that is called separately when needed.
function foo ($arg _1, $arg _2, ..., $arg _n) {
Keyword function name parameter parameter
echo "Example function.\n"; This is a custom function body
return $retval;
}
When a function is called, arguments are passed to the function, the arguments passed in are called arguments, and the arguments to the function definition are formal parameters. Parameters are passed by value, passed by reference, and default parameter 3.
1. Passing by value means that the value of the argument is copied to the corresponding parameter, and the operation inside the function is for the parameter. The result of the operation does not affect the actual argument, that is, the value of the argument does not change after the function returns. This means that the value of the argument is not changed by the function, but the value of the function output is changed by the parameters passed in.
2. Passing by reference means that the memory address of the argument is passed to the formal parameter, and all operations inside the function affect the value of the argument, and the value of the argument is changed, and the reference is passed by adding the & number to the original base on the value.
3. Default parameters, default parameters, and optional parameters, you can specify that a parameter is an optional parameter, place an optional parameter at the end of the parameter list, and specify that its default value is empty. The default parameter must be placed to the right of a non-default parameter, or the function may have an error, and the default value can also be passed by reference.
2. function return value
1. The return value of the function, typically, the way the function passes the return value to the caller is by using the keyword return
Return () returns the value of the function to the caller of the function, returns the program control to the caller's scope, and terminates execution of the script if the return () keyword is used within the global scope.
The return statement can return only one parameter, that is, it can return only one value, cannot return more than one at a time, and if you want to return multiple results, define an array in the function to store the return value back in the array.
2. Variable functions
The invocation of a function is implemented by changing the name of the variable, and by adding a pair of parentheses after the variable name, PHP will automatically look for the same function as the variable name and execute it, if it cannot find an error.
For example: function Go () {
Echo go;
}
function Come () {
echo come;
}
$fun = "Go";
$fun ();
$fun = "Come";
$fun ();
3. Reference to a function
Pass parameters by reference to modify the contents of an argument. References can be used not only for ordinary variables, function arguments, but also for functions themselves, references to functions, and references to functions that return results. The reference function is to define a function, precede the function name with a & character, and then refer to the function through the variable $str, and finally output the variable $STR.
example, function &example ($tmp =0) {
return $tmp;
}
$str = &example ("See");
echo $str. "
";
4. dereference
When a reference is not required, you can dereference and dereference using the unset () function, which simply breaks the binding between the variable name and the content of the variable, rather than destroying the variable content.
PHP variable function library
Commonly used for:
The Isset () function checks whether a variable is set, that is, whether it is assigned a value.
The setting returns TRUE, otherwise false is returned. Isset () can only be used for variables, because passing any other parameter will result in parsing errors. To detect if a constant is set, use the defined () function.
The empty () function checks if a variable is empty, returns true for NULL, or returns false
The GetType () function gets the type of the variable.
Var_dump information about the print variable.
Common String Function libraries
Explode delimited string
Date Time Function library
checkdate Verifying date Validity
Mktime used to return a date for a Unix timestamp
Library of Mathematical functions
Floor to achieve the rounding method rounding
Fmod returns the floating-point remainder of the division.
File system function Library
fopen () is used to open a file and return an identity pointer to the file, which is local and remote.
mkdir New Directory
MySQL function library
There are a number of functions for each function, and here are just a few examples.
Review the content of this section:
1. Defining and invoking functions
2. Passing parameters between functions, passing by value, referencing, default
3. Return the value from the function, return
4. Variable functions
5. Reference to a function
6. dereference
7.php variable function library, Common, String, datetime, math, file system, MySQL function library
The author "technology is king"
http://www.bkjia.com/PHPjc/478640.html www.bkjia.com true http://www.bkjia.com/PHPjc/478640.html techarticle functions are divided into two kinds: system intrinsic function and user custom function. A function is a function that writes a piece of code or a function that is often used in everyday life. Call when needed, call ...