The difference between PHP value passing and reference passing

Source: Internet
Author: User

The difference between PHP value passing and reference passing. When to pass a value when to pass a reference

(1) Passing by value: Any changes to values within the function scope are ignored outside the function

(2) Passing by reference: Any change to a value within a function can also reflect these changes outside the function

(3) Advantages and disadvantages:

A: When passing by value, PHP must copy the value. This can be a costly operation, especially for large strings and objects.

B. Passing by reference does not require copying values, which is good for performance gains.

1 <?Php2Header (' Content-type:text/html;charset=utf-8 ');34//Explore the default delivery mode for array, null, object, resource5Conclusion (1) array By default is a value pass, which can be referenced by adding & passing6(2) Null default is value passing, plus & can refer to pass7(3) A resource is a value pass. You can refer to a pass by adding &8(4) The object is also passed by default, but object value passing is an object identifier9$hero =Array (' No1 ' + ' Batman ', ' no2 ' = ' Superman ');1011$hero 2 = &$hero;12$hero 2[' no1 ' = ' Spider-Man ';13Echo ' <pre> ';14Var_dump ($hero);  var_dump ($hero 2);    $a = null;  $b = &$a;  $b = ' abc ';   var_dump ($a, $b);             

The difference between PHP value passing and reference passing

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.