Does PHP use & to refer to the value of good or global good? Why

Source: Internet
Author: User
Keywords Php
$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();    
  • Related Article

    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.