php function_PHP tutorial

WBOY
Release: 2016-07-13 17:46:00
Original
742 people have browsed it

Functions are divided into two types: system internal functions and user-defined functions. A function is to write a piece of code or a function that is often used in daily life as a function. Call when needed. The purpose of calling functions is to simplify the burden of programming, reduce the amount of code and improve efficiency, so as to increase code reusability and avoid repeated development.

1. Define and call functions, and how to pass values ​​between functions.

A function is to write some repeatedly used functions into an independent code block and call it separately when needed.
function foo ($arg_1, $arg_2, ..., $arg_n){
Keyword Function name Parameter Parameter Parameter

echo "Example function.n"; This is the body of the custom function
Return $retval;
}

When calling a function, you need to pass parameters to the function. The parameters passed in are called actual parameters, and the parameters defined by the function are formal parameters. There are three ways to pass parameters: pass by value, pass by reference, and default parameters.

1. Passing by value means copying the value of the actual parameter to the corresponding formal parameter, and the operations inside the function are performed on the formal parameter. The result of the operation will not affect the actual parameters, that is, the values ​​of the actual parameters will not change after the function returns. That is to say, the value of the actual parameter will not change due to the change of the function, but the value output by the function will change according to the passed parameter.

2. Passing by reference is to pass the memory address of the actual parameter to the formal parameter. At this time, all operations inside the function will affect the value of the actual parameter. The value of the actual parameter will change after returning. The passing by reference method is to pass Just add an ampersand (&) to the original value.

3. Default parameters, default parameters and optional parameters, you can specify a parameter as an optional parameter, place the optional parameter at the end of the parameter list, and specify its default value to be empty. Default parameters must be placed to the right of non-default parameters, otherwise the function may error. Default values ​​can also be passed by reference.

2. Function return value

1. The return value of a function. Usually, the way a function passes the return value to the caller is to use the keyword return

return() returns the value of the function to the caller of the function, that is, returns program control to the caller's scope. If the return() keyword is used in the global scope, the execution of the script will be terminated.
The return statement can only return one parameter, that is, it can only return one value, and cannot return multiple results at one time. If you want to return multiple results, you must define an array in the function, store the return value in the array, and return.

2. Variable function

The function is called by changing the variable name. By adding a pair of parentheses after the variable name, PHP will automatically search for a function with the same name as the variable and execute it. If it is not found, an error will be reported.
For example: function go(){
echo go;
}
function come(){
echo come;
}
$fun="go";
$fun();
$fun="come";
$fun();

3. Reference to function

Passing parameters by reference can modify the content of the actual parameters. References can be used not only for ordinary variables and function parameters, but also for the function itself. A reference to a function is a reference to the result returned by the function. To reference a function is to add the ampersand (&) before the function name, then reference the function through the variable $str, and finally output the variable $str.
Example, function &example($tmp=0){
return $tmp;
}
$str = &example("Saw it");
echo $str."

";


4. Unreference

When the reference is no longer needed, the reference can be dereferenced. To dereference, use the unset() function. It only breaks the binding between the variable name and the variable content, rather than destroying the variable content.

php variable function library

Commonly used:

The isset() function checks whether the variable is set, that is, whether it is assigned a value.
If set, return TRUE, otherwise return false. isset() can only be used with variables, since passing any other arguments will cause a parsing error. If you want to check whether a constant has been set, use the defined() function.
The empty() function checks whether a variable is empty and returns TRUE if it is empty, otherwise it returns FALSE
The gettype() function gets the type of the variable.
var_dump prints information about variables.

Commonly used string function libraries
explode delimited string

Date and time function library
checkdate Verify date validity
mktime is used to return the unix timestamp of a date

Mathematical function library
floor implements rounding by rounding
fmod returns the floating point remainder of division.

File system function library
fopen() is used to open a file and return the identification pointer of the file. The file can be local or remote.
mkdir creates a new directory

mysql function library

There are many types of functions, but here are just a few examples.

Review this section:
1. Define and call functions
2. Pass parameters between functions, pass by value, reference, default
3. Return value from function, return
4.Variable function
5. Reference to function
6. Unreference
7.php variable function library, commonly used, string, date and time, mathematics, file system, mysql function library
Author "Technology is King"

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478640.htmlTechArticleFunctions are divided into system internal functions and user-defined functions. A function is to write a piece of code or a function that is often used in daily life as a function. Make calls when needed, call...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!