Is it better for php to use & reference and pass the value to the global system? Why?

Source: Internet
Author: User
$ 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.globalThe 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 defineglobalWhen using a variable, you need to consider whether the same name is defined and used in the entire system.globalVariable.
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();    

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.