PHP value transfer and reference transfer

Source: Internet
Author: User
PHP value transfer and reference transfer when we want to operate an external variable in the function, the following methods are used:

Reference transfer

Function pass_by_reference (& $ num) {// note that $ num is the reference of the parameter you passed. // other variables can also reference this variable to point to the parameter address $ temp = & $ num; $ temp ++;} $ arg = 1; pass_by_reference ($ arg); echo $ arg; // 2pass_by_reference ($ arg); echo $ arg; // 3

Global Array

Function add () {$ GLOBALS ['num'] ++;} $ num = 0; add (); echo $ num;

The most interesting representation of global variable declaration is to declare a reference with the same name for the variables in the global array.

Function add () {// In fact, the operating mechanism of the global modifier here is $ num = & $ GLOBAL ['num'], which defines a global variable $ num reference global $ num; $ num ++;} $ num = 0; add (); echo $ num;

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.