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;