Php variable functions and function references and disreference instances

Source: Internet
Author: User
In php, variable functions are supported. First, some functions are defined, and a variable is declared. different functions are called using variables, and variable values are assigned again. The following describes how to use a variable function through an instance. Variable functions

In php, variable functions are supported. First, some functions are defined, and a variable is declared. different functions are called using variables, and variable values are assigned again. The following describes how to use a variable function through an instance. In this example, we first define three functions, then declare a variable and assign values, and use the variable function to access different three functions. we need to assign values to the variable again and again.

The sample code is as follows:

 ";} Function go ($ name =" tom ") {echo" $ name goes out
";} Function back ($ str) {echo" re-entry $ str
";}$ Func =" come "; // declare a variable and assign a value to $ func (); // use the variable function to call the function come () $ func =" go "; // assign a new value to the variable $ func ("jane"); // use the variable function to call the function go () $ func = "back "; // assign a value to the variable $ func ("jack"); // use the variable function to call the function back ()?>

The running result is as follows:

Function Reference

You can modify the content of a real parameter by reference when passing parameters between functions. References can be used not only for common variables and function parameters, but also for functions themselves. A reference to a function is a reference to the result returned by the function.

Next we will use an example to illustrate how to define a function. here we need to add the "&" character before the function name. then, the variable $ str will reference this function, the output variable $ str is actually the value of $ tmp.

The instance code is as follows:

 

Output result:Hello word

Note: Unlike parameter passing, function reference must be used in both places.&To indicate that a reference is returned.

Cancel reference

When no reference is required in the actual operation, you can cancel the reference of the parameter transfer method. you only need to use the PHP function. here, the unset () function is used to cancel the reference, it only disconnects the binding between the variable name and the variable content, rather than destroying the variable content.

The following example illustrates how to declare a variable and a variable reference first, output the reference, cancel the reference, and call the reference and the original variable again. It can be seen that the original variable is not affected after the reference is canceled.

The instance code is as follows:

 "; unset($math); echo "\$math is: ".$math . "
"; echo "\$num is: " .$num;?>

The running result is:

$math is: 123$math is: $num is: 123

The above is an example of the variable functions of php user-defined functions and the content of function reference and unreference. in the next chapter, we will start to learn the scope of php user-defined function variables.

[Recommended tutorials]

1. php1.cn Dugu jiujian (4)-php video tutorial

2. php programming from getting started to mastering a full set of video tutorials

3. php video tutorial

The above is the detailed description of php variable functions and function reference and unreference instances. For more information, see other related articles in the first PHP community!

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.