$ Con10; $ row20; functionnumbers (& amp; $ con, $ row) {code ...}} numbers ($ con, $ row); echo $ con; $ con = 10;
$ Row = 20;
Function numbers (& $ con, $ row ){
// global $con; $con = pow($con,2); $row = pow($row,2);
}
Numbers ($ con, $ row );
Echo $ con;
Reply content:
$ Con = 10;
$ Row = 20;
Function numbers (& $ con, $ row ){
// global $con; $con = pow($con,2); $row = pow($row,2);
}
Numbers ($ con, $ row );
Echo $ con;
RecommendedReference passed Value
. In most cases, the referenced value will be processed in code that is very close to the function call, and the scope of the variable is very small.global
The scope of the defined variables is almost the same as that of the global variables. A slight carelessness may cause unnecessary problems. For a simple example, you defineglobal
When using a variable, you need to consider whether the same name is defined and used in the entire system.global
Variable.
This is why global variables are discarded when reference values are passed in the face object programming. In addition, objects and resources in PHP are passed by reference by default.
Reference: The address is passed.
Functions need to be used in multiple places, so global is not recommended for applicability.
If the object is referenced by an array, if the parameter is large, it will be referenced.
In terms of reusability, it is of course a good reference transfer.
In this case, you can write what you want. I can skip global variables instead of global variables.
If you can only select either of them, we recommend that you reference them.
It is more universal to use referenced functions. For example, if one day you suddenly want to change a local variable for this function, then the global function will not work, and the referenced version can still be used.
Cannot I use class attributes?
If there is a value that requires frequent processing, it is declared as a class attribute, remember to say in the psr-0 a file a class, so the use of attributes in a class is enough, $ this-> is enough when you use it. please correct me:
con,2); $row = pow($this->row,2); echo $con; } } (new test())->numbers();