Php function _ PHP Tutorial

Source: Internet
Author: User
Php functions. Functions can be divided into system internal functions and user-defined functions. A function is to write a piece of code or a function that is frequently used in daily life as a function. Call a function as needed. the called function can be divided into system internal functions and user-defined functions. A function is to write a piece of code or a function that is frequently used in daily life as a function. Call a function when necessary. the purpose of calling a function is to simplify programming, reduce the amount of code and improve efficiency, increase code reusability, and avoid repeated development.

1. define and call functions, and how to pass values between functions.

A function is used to write repeated functions into an independent code block, which is called independently when needed.
Function foo ($ arg_1, $ arg_2,..., $ arg_n ){
Keyword function name parameter

Echo "Example function. \ n"; this is the subject of the custom function.
Return $ retval;
}

When calling a function, you need to pass parameters to the function. the passed parameters are called real parameters, and the function-defined parameters are form parameters. Parameters can be transmitted by value, by reference, or by default.

1. pass by value refers to copying the value of the real parameter to the corresponding shape parameter, and the operation inside the function is performed on the shape parameter. The result of the operation does not affect the real parameter, that is, the value of the real parameter will not change after the function returns. That is to say, the value of the real parameter will not change because of the change of the function, but the value of the function output is changed according to the input parameter.

2. the reference transfer method is to pass the memory address of the real parameter to the form parameter. in this case, all operations in the function will affect the value of the real parameter, and the value of the real parameter will change after the result is returned, the reference transfer method is to add the & sign to the original base when passing the value.

3. default parameters, default parameters, and optional parameters. you can specify a parameter as an optional parameter. you can leave the optional parameter at the end of the parameter list and specify its default value as null. Default parameters must be placed on the right of non-default parameters. otherwise, function errors may occur. the default value can also be passed through reference.

2. function return value

1. the return value of a function. Generally, the function transmits the return value to the caller by using the keyword return

Return () returns the value of the function to the caller of the function, that is, the control of the program is returned 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 return only one parameter, that is, only one value. multiple parameters cannot be returned at a time. to return multiple results, define an array in the function, store the returned values in the array and return them.

2. variable functions

The function is called by changing the variable name. by adding a pair of parentheses after the variable name, php will automatically search for the function with the same variable name and execute it, if not, an error is returned.
Example: function go (){
Echo go;
}
Function come (){
Echo come;
}
$ Fun = "go ";
$ Fun ();
$ Fun = "come ";
$ Fun ();

3. Function Reference

Passing parameters by reference can modify the content of real parameters. A reference can be used not only for common variables, function parameters, but also for functions themselves. a reference to a function is a reference to the function returned results. When defining a function, add the & symbol before the function name, reference the function through the variable $ str, and output the variable $ str.
For example, function & example ($ tmp = 0 ){
Return $ tmp;
}
$ Str = & example ("see ");
Echo $ str ."

";


4. cancel reference

When no reference is required, you can cancel the reference and use the unset () function to cancel the reference. it only disconnects the binding between the variable name and the variable content, rather than destroying the variable content.

Php variable Library

Common:

The isset () function checks whether the variable is set, that is, whether it is assigned a value.
If this parameter is set, TRUE is returned. otherwise, false is returned. Isset () can only be used for variables, because passing any other parameter will cause a parsing error. To check whether a constant has been set, use the defined () function.
Empty () function checks whether a variable is null. if it is null, TRUE is returned. otherwise, FALSE is returned.
The gettype () function gets the type of the variable.
Var_dump prints information about the variable.

Common string function libraries
Explode separator string

Date and time function library
Checkdate verification date validity
Mktime is used to return the unix timestamp of a date.

Mathematical function library
Floor implements rounding
Fmod returns the floating point remainder of the division.

File system function library
Fopen () is used to open a file and return the identification pointer of the file. This file is local and remote.
Create a directory using mkdir

Mysql function library

Each function has many numbers. here we only provide 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 functions
5. Function Reference
6. cancel the reference.
7. php variable function library, common, string, date and time, mathematics, file system, mysql function library
Author "technology is King"

Bytes. A function is to write a piece of code or a function that is frequently used in daily life as a function. Call it when necessary. call...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.