What is a PHP call function, parameter passing, variable function, reference

Source: Internet
Author: User
1. Defining and invoking functions

Function Concept: Write some of the reused functionality in a separate code block, and call it separately when needed.

The basic syntax format for creating a function is:       

function Fun_name ($str 1, $stgr 2 ... $strn) {fun_body; }

Parameter description:

function: The keyword that must be used to declare a custom function.

Fun_name: The name of the custom function.

$str 1 ... $strn: The parameter for the function.

Fun_body: The body of the custom function, which is the function implementation part.

Function call: When the function is defined, all that is required is to call the function. It is very simple to invoke a function, simply by referencing the function name and giving the correct arguments to complete the function invocation.

2. Passing parameters between functions

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. By Value delivery method

The value of the argument is copied to the corresponding parameter, and the action inside the function is for the parameter, and the result of the operation does not affect the argument, that is, the value of the argument does not change after the function returns.

2. By-Reference delivery method

Passing by reference is the passing of the memory address of the argument to the formal parameter. At this point, all operations inside the function will affect the value of the argument, and when returned, the value of the argument will change. The way to pass the reference is to add the & number on the original basis when the value is passed.

3. Default parameters (Optional parameters)

There is also a way to set parameters that you can select. 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 be empty.

3. Returning a value from a function

Typically, a function passes the return value to the caller by using the keyword return ().

Return () returns the value of the function to the caller of the function, returning control of the program to the scope of the caller. If the return () keyword is used within the global scope, execution of the script is terminated.

The return statement can return only one parameter, that is, it can return only one value, and cannot return more than one at a time. To return multiple results, define an array in the function to store the return value in the array.

4. Variable function

PHP supports variable functions. The following is an example of how variable functions are applied. Slightly

5. References to functions

References 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 result of the function's return.

6. dereference

You can dereference a reference when you no longer need it. Dereferencing uses the unset () function, which simply breaks the binding between the variable name and the variable content, rather than destroying the variable content.

Related Article

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.