$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;
Recommended Use 引用传值
. In most cases, the referenced value will be processed in code that is close to the function call, the scope of the variable is very small, and global
the defined variable scope is almost identical to the global variable, and a slight carelessness can cause unnecessary problems. As a simple example, when you define a global
variable, you need to consider whether there is a place in the whole system that also defines and uses the same name global
variable.
This is why the object is programmed with reference values, and the reason for discarding global variables. In addition, the objects and resources in PHP are passed by default by reference.
Reference, passing the address
Functions need to be used in multiple places, applicability is not recommended for global
object is a reference to an array of arguments if it's bigger, it doesn't matter if you use a few data.
From the perspective of reusability is of course a good reference.
You can write this kind of place if you want to write it. I can do it anyway. Global variables are not used.
If only two is selected, I recommend the reference.
Using a referenced function is better. For example, one day you suddenly want this function to change a local variable, then the function of global is not, and the reference version can still be used.
Can't I use class properties?
If you have a value that requires frequent processing, declare it as a class property, remember that Psr-0 says a file in a class, so a class inside with the property is enough, use the time $this-> is good, welcome correction:
con,2); $row = pow($this->row,2); echo $con; } } (new test())->numbers();